当前位置:网站首页>Using SqlCommand objects in code
Using SqlCommand objects in code
2022-07-01 23:43:00 【Quzhou Xiaofeng】
4.1 Used in code SqlCommand object
4.1.1 establish sqlCommand object
// Create without parameters
sqlCommand cmd;
cmd = new sqlCommand();
cmd.Connection = cn;
cmd.CommandText = strSQL;
// Use parameters to construct
sqlCommand cmd = new sqlCommand( strSQL.cn);
// use connection Of CreateCommand To construct the
cmd = cn.CreateCommand();
cmd.CommandText = strSQL;
4.1.3 Execute the query that returns rows
sqlCommand cmd = cn.CreateCommand();
cmd.CommandText = "select * From ...";
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
Console.Write(dr["UserName"]);
4.1.4 Get a single value
strSQL = "select count(*) from tabname";
sqlCommandText = new sqlCommand(strSQL,cn);
int icount = (int)cmd.ExecuteScalar();
4.1.5 Execute queries that do not return result sets
strSQL ="Update table set field01=100"
sqlCommand cmd = new sqlCommand(strSQL,cn);
int iRow= cmd.ExecuteNonQuery(); // The return value is the number of rows affected , Reflect the successful implementation
4.1.6 Execute batch operation query
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();// Return the total number of rows modified by each query ,
utilize StatementCompleted Event can determine the number of rows modified by each query in batch operation
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;// The number of rows affected by each query can be reflected here
}
边栏推荐
- Depth first search and breadth first search of graph traversal
- Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
- 华为HMS Core携手超图为三维GIS注入新动能
- Create Ca and issue certificate through go language
- y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
- const // It is a const object...class nullptr_t
- Know --matplotlib
- MT manager test skiing Adventure
- JPA handwritten SQL, received with user-defined entity classes
- PyTorch学习记录
猜你喜欢
Paramètres communs de matplotlib
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
Overview of edge calculation
Kubernetes resource object introduction and common commands (III)
Redis AOF日志
距离度量 —— 汉明距离(Hamming Distance)
使用 pair 做 unordered_map 的键值
Distance measurement - Hamming distance
TS初次使用、ts类型
Redis data types and application scenarios
随机推荐
Li Kou today's question -241 Design priorities for operational expressions
mt管理器测试滑雪大冒险
Current situation and future development trend of Internet of things
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
Daily three questions 6.29
Use vb Net to convert PNG pictures into icon type icon files
Redis AOF log
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
cookie、session、tooken
.env.xxx 文件,加了常量,却undefined
RPA教程01:EXCEL自动化从入门到实操
Algolia's search needs are almost closed
边缘计算概述
excel如何打开100万行以上的csv文件
Why is PHP called hypertext preprocessor
Switch to software testing, knowing these four points is enough!
ConcurrentSkipListMap——跳表原理
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
深度学习 | 三个概念:Epoch, Batch, Iteration
Why does blocprovider feel similar to provider?