当前位置:网站首页>在代码中使用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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- openwrt 开启KV漫游
- ShanDong Multi-University Training #3
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- notBlank 和 notEmpty
- What is the difference between memory leak and memory overflow?
- Matplotlib common settings
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- Redis AOF日志
- Huisheng Huiying 2022 intelligent, fast and simple video editing software
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
猜你喜欢
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]

What professional classification does the application of Internet of things technology belong to

Redis master-slave synchronization

Current situation and future development trend of Internet of things

2022 crane driver (limited to bridge crane) examination questions and simulation examination

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

Linux foundation - centos7 offline installation of MySQL

Redis RDB快照

【ES实战】ES上的安全性运行方式

Redis数据类型和应用场景
随机推荐
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
【无标题】
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Depth first search and breadth first search of graph traversal
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Commemorate becoming the first dayus200 tripartite demo contributor
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Redis data types and application scenarios
2021 RoboCom 世界机器人开发者大赛-高职组复赛
STM32F030F4驱动TIM1637数码管芯片
Experience of practical learning of Silicon Valley products
Microservice stability management
Paramètres communs de matplotlib
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
学成在线案例实战
神经网络物联网的未来趋势与发展
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
The difference between timer and scheduledthreadpoolexecutor
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题