当前位置:网站首页>在代码中使用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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- [must] bm41 output the right view of the binary tree [medium +]
- Postgresql源码(58)元组拼接heap_form_tuple剖析
- SWT / anr problem - SWT causes kernel fuse deadlock
- Is there a piece of code that makes you convinced by human wisdom
- What category does the Internet of things application technology major belong to
- 问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
- 物联网开发零基础教程
- Matplotlib常用图表
- MySQL binlog cleanup
猜你喜欢

问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64

深度学习 | 三个概念:Epoch, Batch, Iteration

有没有一段代码,让你为人类的智慧所折服

【必会】BM41 输出二叉树的右视图【中等+】

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

CADD course learning (3) -- target drug interaction

How to display real-time 2D map after rviz is opened

边缘计算概述

Matplotlib common settings

2021 RoboCom 世界机器人开发者大赛-本科组初赛
随机推荐
notBlank 和 notEmpty
Is there a piece of code that makes you convinced by human wisdom
Postgresql源码(57)HOT更新为什么性能差距那么大?
Postgresql随手记(10)动态执行EXECUTING语法解析过程
PostgreSQL notes (10) dynamically execute syntax parsing process
ARP message header format and request flow
物联网开发零基础教程
神经网络物联网的未来趋势与发展
Practical application and extension of plain framework
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
The digital summit is popular, and city chain technology has triggered a new round of business transformation
Huisheng Huiying 2022 intelligent, fast and simple video editing software
【C#】依赖注入及Autofac
Commemorate becoming the first dayus200 tripartite demo contributor
Zero foundation tutorial of Internet of things development
[micro service sentinel] @sentinelresource details
Redis AOF日志
[micro service sentinel] sentinel integrates openfeign
安全协议重点
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c