当前位置:网站首页>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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- 力扣今日题-241. 为运算表达式设计优先级
- Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
- [applet] realize the left and right [sliding] list through the scroll view component
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- ShanDong Multi-University Training #3
- Redis AOF log
- SWT / anr problem - SWT causes low memory killer (LMK)
- 使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
- Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
猜你喜欢
Material Design组件 - 使用BottomSheet展现扩展内容(一)
软件架构的本质
Is there a piece of code that makes you convinced by human wisdom
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
CADD course learning (3) -- target drug interaction
2022年R1快开门式压力容器操作考题及答案
Linux基础 —— CentOS7 离线安装 MySQL
Redis RDB snapshot
Matplotlib common charts
Matplotlib常用設置
随机推荐
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
Linux foundation - centos7 offline installation of MySQL
Paramètres communs de matplotlib
Daily three questions 6.30
Zero foundation tutorial of Internet of things development
常见的积分商城游戏类型有哪些?
SWT/ANR问题--SWT 导致 low memory killer(LMK)
Matplotlib常用设置
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
2021 RoboCom 世界机器人开发者大赛-本科组初赛
物联网现状及未来发展趋势
2022安全员-C证考试题模拟考试题库及模拟考试
The difference between timer and scheduledthreadpoolexecutor
为什么PHP叫超文本预处理器
vs2015 AdminDeployment.xml
mt管理器测试滑雪大冒险
物联网技术应用属于什么专业分类
What is mosaic?
2021 RoboCom 世界机器人开发者大赛-高职组初赛
How to display real-time 2D map after rviz is opened