当前位置:网站首页>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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- mt管理器测试滑雪大冒险
- MySQL binlog cleanup
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- Daily three questions 6.30 (2)
- 小程序表单校验封装
- Depth first search and breadth first search of graph traversal
- 神经网络物联网的未来趋势与发展
- SWT/ANR问题--SWT 导致 kernel fuse deadlock
- 图的遍历之深度优先搜索和广度优先搜索
- What is the mosaic tailgate?
猜你喜欢

有没有一段代码,让你为人类的智慧所折服

软件架构的本质

What is the relationship between modeling and later film and television?

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

Matplotlib常用图表

Experience of practical learning of Silicon Valley products

Redis AOF日志

Material Design组件 - 使用BottomSheet展现扩展内容(一)

ConcurrentSkipListMap——跳表原理

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
随机推荐
【ES实战】ES上的安全性运行方式
JS - use of arguments
Notblank and notempty
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
认识--Matplotlib
每日三题 6.28
Three development trends of enterprise application from the perspective of the third technological revolution
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
ShanDong Multi-University Training #3
How to display real-time 2D map after rviz is opened
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
[micro service sentinel] @sentinelresource details
Redis AOF日志
Applet form verification encapsulation
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
物联网技术应用属于什么专业分类
转行软件测试,知道这四点就够了!
Which securities company is better and which is safer to open a securities account
Redis 主从同步