当前位置:网站首页>ADO.NET之sqlCommand对象
ADO.NET之sqlCommand对象
2022-07-01 23:23:00 【衢州小风风】
4.1在代码中使用SqlCommand对象
4.1.1创建sqlCommand对象
//不使用参数创建
sqlCommand cmd;
cmd = new sqlCommand();
cmd.Connection = cn;
cmd.CommandText = strSQL;
//使用参数来构造
sqlCommand cmd = new sqlCommand( strSQL.cn);
//用connection的CreateCommand来构造
cmd = cn.CreateCommand();
cmd.CommandText = strSQL;
4.1.3 执行返回行的查询
sqlCommand cmd = cn.CreateCommand();
cmd.CommandText = "select * From ...";
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
Console.Write(dr["UserName"]);
4.1.4 获取单一值
strSQL = "select count(*) from tabname";
sqlCommandText = new sqlCommand(strSQL,cn);
int icount = (int)cmd.ExecuteScalar();
4.1.5执行不返回结果集的查询
strSQL ="Update table set field01=100"
sqlCommand cmd = new sqlCommand(strSQL,cn);
int iRow= cmd.ExecuteNonQuery(); //返回值是受影响的行数,反应执行成功的情况
4.1.6 执行批量操作查询
strSQL ="update table01 set field01 =001 .." +
"update table02 set field01 =002 .." +
"update table03 set field01 =003 .."
sqlCommand cmd = new sqlCommand(strSQL,cn);
int icount = cmd.ExecuteNonQuery();//返回各个查询所修改的总行数,
利用StatementCompleted事件可以确定批量操作中各个查询所修改的行数
sqlCommand cmd = new sqlCommand(strSQL,cn);
cmd.StatementCompleted += new StatementCompletedEventHandler(HandleStatementCompleted);
int irow = cmd.ExcuteNonQuery();
static void HandleStatementCompleted(object sender,StatementCompletedEventArgs e)
{
int row = e.RecordCount;//这里能反应出每个查询受影响的行数
}
边栏推荐
- Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- sql 优化
- SWT / anr problem - SWT causes kernel fuse deadlock
- [applet] realize the left and right [sliding] list through the scroll view component
- 2021 RoboCom 世界机器人开发者大赛-高职组复赛
- Switch to software testing, knowing these four points is enough!
- vs2015 AdminDeployment.xml
- Practical application and extension of plain framework
- Paramètres communs de matplotlib
猜你喜欢
Distance measurement - Hamming distance
Huisheng Huiying 2022 intelligent, fast and simple video editing software
SWT / anr problem - SWT causes kernel fuse deadlock
[applet] realize the left and right [sliding] list through the scroll view component
有没有一段代码,让你为人类的智慧所折服
云信小课堂 | IM及音视频中常见的认知误区
2022年起重机司机(限桥式起重机)考试试题及模拟考试
【.Net Core】程序相关各种全局文件
2022 R1 fast opening pressure vessel operation test questions and answers
flutter Unable to load asset: assets/images/888. png
随机推荐
字典、哈希表、数组的概念
Airserver latest win64 bit personal screen projection software
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
flutter Unable to load asset: assets/images/888. png
Which securities company is better and which is safer to open a securities account
写给当前及未来博士研究生一些建议整理分享
[micro service sentinel] @sentinelresource details
Redis AOF log
Future trend and development of neural network Internet of things
What is the difference between memory leak and memory overflow?
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Daily three questions 6.30 (2)
Redis AOF日志
Create Ca and issue certificate through go language
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
[untitled]
SWT / anr problem - SWT causes kernel fuse deadlock
物联网开发零基础教程
typescript枚举
认识--Matplotlib