当前位置:网站首页>在代码中使用SqlCommand对象
在代码中使用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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- 云信小课堂 | IM及音视频中常见的认知误区
- Current situation and future development trend of Internet of things
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- flutter Unable to load asset: assets/images/888. png
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- SWT/ANR问题--SWT 导致 low memory killer(LMK)
- Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
- Matplotlib常用設置
- Commemorate becoming the first dayus200 tripartite demo contributor
- Typescript enumeration
猜你喜欢
Three development trends of enterprise application from the perspective of the third technological revolution
Practical application and extension of plain framework
Distance measurement - Hamming distance
What is the relationship between modeling and later film and television?
What is the mosaic tailgate?
Notblank and notempty
深度学习 | 三个概念:Epoch, Batch, Iteration
flutter Unable to load asset: assets/images/888. png
Yunxin small class | common cognitive misunderstandings in IM and audio and video
Redis数据类型和应用场景
随机推荐
openwrt 开启KV漫游
Distance measurement - Hamming distance
Huisheng Huiying 2022 intelligent, fast and simple video editing software
excel如何打开100万行以上的csv文件
第六章 数据流建模
Typescript enumeration
从第三次技术革命看企业应用三大开发趋势
Redis RDB快照
mt管理器测试滑雪大冒险
Redis data types and application scenarios
深度学习 | 三个概念:Epoch, Batch, Iteration
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
What is the difference between memory leak and memory overflow?
The difference between timer and scheduledthreadpoolexecutor
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
通过Go语言创建CA与签发证书
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Matplotlib常用設置
Redis RDB snapshot