当前位置:网站首页>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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- 边缘计算概述
- 物联网应用技术专业是属于什么类
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- [micro service sentinel] @sentinelresource details
- flutter Unable to load asset: assets/images/888. png
- Which securities company is better and which is safer to open a securities account
- Yunxin small class | common cognitive misunderstandings in IM and audio and video
- Redis RDB快照
- Postgresql源码(57)HOT更新为什么性能差距那么大?
- Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
猜你喜欢

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

The digital summit is popular, and city chain technology has triggered a new round of business transformation

Know --matplotlib

纪念成为首个DAYUs200三方demo贡献者

Three development trends of enterprise application from the perspective of the third technological revolution

What category does the Internet of things application technology major belong to

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c

2022年R1快开门式压力容器操作考题及答案

STM32F030F4驱动TIM1637数码管芯片

Matplotlib common charts
随机推荐
Future trend and development of neural network Internet of things
2021 RoboCom 世界机器人开发者大赛-高职组初赛
TS初次使用、ts类型
软件架构的本质
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
Zero foundation tutorial of Internet of things development
神经网络物联网的发展趋势和未来方向
Practical application and extension of plain framework
PostgreSQL source code (57) why is the performance gap so large in hot update?
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
Win 10 mstsc connect RemoteApp
Linux foundation - centos7 offline installation of MySQL
Matplotlib common charts
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Airserver latest win64 bit personal screen projection software
Create Ca and issue certificate through go language
Three development trends of enterprise application from the perspective of the third technological revolution
The difference between timer and scheduledthreadpoolexecutor
Current situation and future development trend of Internet of things
PostgreSQL notes (10) dynamically execute syntax parsing process