当前位置:网站首页>在代码中使用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;//这里能反应出每个查询受影响的行数
}
边栏推荐
- CADD course learning (3) -- target drug interaction
- 问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
- Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
- ARP报文头部格式和请求流程
- 物联网技术应用属于什么专业分类
- 有没有一段代码,让你为人类的智慧所折服
- Redis data types and application scenarios
- 每日三题 6.30
- STM32F030F4驱动TIM1637数码管芯片
- How to display real-time 2D map after rviz is opened
猜你喜欢
TS初次使用、ts类型
Matplotlib常用图表
flutter Unable to load asset: assets/images/888. png
云信小课堂 | IM及音视频中常见的认知误区
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
What is the relationship between modeling and later film and television?
What professional classification does the application of Internet of things technology belong to
学成在线案例实战
Postgresql源码(57)HOT更新为什么性能差距那么大?
CADD course learning (3) -- target drug interaction
随机推荐
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
Redis RDB snapshot
CADD course learning (3) -- target drug interaction
神经网络物联网的未来趋势与发展
[applet] realize the left and right [sliding] list through the scroll view component
2022年起重机司机(限桥式起重机)考试试题及模拟考试
Typescript enumeration
字典、哈希表、数组的概念
Practical application and extension of plain framework
Linux基础 —— CentOS7 离线安装 MySQL
Matplotlib常用設置
每日三题 6.28
mt管理器测试滑雪大冒险
The difference between timer and scheduledthreadpoolexecutor
物联网现状及未来发展趋势
【C#】依赖注入及Autofac
Practical application and extension of plain framework
Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
Distance measurement - Hamming distance
PostgreSQL notes (10) dynamically execute syntax parsing process