当前位置:网站首页>在代码中使用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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- mysql binlog的清理
- Who do you want to know when opening a stock account? Is it safe to open an account online?
- typescript枚举
- Redis数据类型和应用场景
- How to display real-time 2D map after rviz is opened
- 【必会】BM41 输出二叉树的右视图【中等+】
- Distance measurement - Hamming distance
- Current situation and future development trend of Internet of things
- Notblank and notempty
- Matplotlib common charts
猜你喜欢

Yunxin small class | common cognitive misunderstandings in IM and audio and video

门级建模—课后习题

Linux foundation - centos7 offline installation of MySQL

SWT / anr problem - SWT causes kernel fuse deadlock

2022年危险化学品经营单位安全管理人员考试题及在线模拟考试

物联网应用技术专业是属于什么类

2021 RoboCom 世界机器人开发者大赛-高职组复赛

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

2022 R1 fast opening pressure vessel operation test questions and answers

Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
随机推荐
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
图的遍历之深度优先搜索和广度优先搜索
Redis AOF日志
Redis RDB快照
URL introduction
The difference between timer and scheduledthreadpoolexecutor
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
SWT/ANR问题--SWT 导致 kernel fuse deadlock
SWT / anr problem - SWT causes low memory killer (LMK)
安全协议重点
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
Postgresql源码(57)HOT更新为什么性能差距那么大?
2021 RoboCom 世界机器人开发者大赛-本科组初赛
Create Ca and issue certificate through go language
Matplotlib常用設置
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
Who do you want to know when opening a stock account? Is it safe to open an account online?
【无标题】
2021 RoboCom 世界机器人开发者大赛-高职组初赛