当前位置:网站首页>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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
- [must] bm41 output the right view of the binary tree [medium +]
- 13 MySQL-约束
- Is there a piece of code that makes you convinced by human wisdom
- 写给当前及未来博士研究生一些建议整理分享
- Why is PHP called hypertext preprocessor
- Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
- 物联网应用技术专业是属于什么类
- 边缘计算概述
- 物联网开发零基础教程
猜你喜欢

Matplotlib常用設置

边缘计算概述

Airserver latest win64 bit personal screen projection software

Commemorate becoming the first dayus200 tripartite demo contributor

2022年起重机司机(限桥式起重机)考试试题及模拟考试

纪念成为首个DAYUs200三方demo贡献者

De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'

安全协议重点

What is mosaic?

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
SWT/ANR问题--SWT 导致 low memory killer(LMK)
[swoole Series 1] what will you learn in the world of swoole?
Concepts of dictionary, hash table and array
plain framework的实际应用和扩展
每日三题 6.30
Depth first search and breadth first search of graph traversal
神经网络物联网的发展趋势和未来方向
What category does the Internet of things application technology major belong to
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
2022 crane driver (limited to bridge crane) examination questions and simulation examination
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
What are the common types of points mall games?
Redis AOF日志
写给当前及未来博士研究生一些建议整理分享
Li Kou today's question -241 Design priorities for operational expressions
[leetcode] length of the last word [58]
Win 10 mstsc connect RemoteApp
2021 RoboCom 世界机器人开发者大赛-高职组复赛