Command
来源:互联网 发布:java 防止盗刷短信 编辑:程序博客网 时间:2024/05/21 06:20
5.1.4 抽水机--Command
Command对象封装了与用户想要完成的动作相关的数据库命令。在一般情况下这些命令就是SQL语句。
1.构造SqlCommand
(1)初始化SqlCommand类的新实例。
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString);
- SqlCommand myCommand = new SqlCommand();
- myCommand.Connection = myConnection;
- myCommand.CommandText = "update P_Product set Name='电脑1' where Id=52";
- myConnection.Open();
- int rows = myCommand.ExecuteNonQuery();
- myConnection.Close();
(2)初始化具有查询文本的 SqlCommand 类的新实例。
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString );
- string strSql = "update P_Product set Name='电脑2' where Id=52";
- SqlCommand myCommand = new SqlCommand(strSql);
- myCommand.Connection = myConnection;
- myConnection.Open();
- int rows = myCommand.ExecuteNonQuery();
- myConnection.Close();
(3)初始化具有查询文本和 SqlConnection 的SqlCommand类实例。
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString );
- string strSql = "update P_Product set Name='电脑3' where Id=52";
- SqlCommand myCommand = new SqlCommand(strSql, myConnection);
- myConnection.Open();
- int rows = myCommand.ExecuteNonQuery();
- myConnection.Close();
(4)初始化具有查询文本、SqlConnection 和 Transaction 的 SqlCommand 类实例。
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString );
- string strSql = "update P_Product set Name='电脑4' where Id=52";
- string strSql2 = "update P_Product set Name='数码4' where Id=53";
- myConnection.Open();
- SqlTransaction myTrans = myConnection.BeginTransaction();
- SqlCommand myCommand = new SqlCommand(strSql, myConnection, myTrans);
- try
- {
- int rows = myCommand.ExecuteNonQuery();
- myCommand.CommandText = strSql2;
- rows = myCommand.ExecuteNonQuery();
- myTrans.Commit();
- myConnection.Close();
- }
- catch
- {
- myTrans.Rollback();
- }
2.建立SqlCommand与SqlConnection的关联
- myCommand.Connection = myConnection;
//或者
- SqlCommand myCommand = myConnection.CreateCommand;
3.设置SqlCommand的查询文本
- myCommand.CommandText = "SELECT * FROM P_Product ";
- //或者第2种构造:
- SqlCommand myCommand = new SqlCommand(strSql);
4.执行命令
SqlCommand方法如表5-1所示。
表5-1 SqlCommand方法
方 法
说 明
ExecuteReader
返回一行或多行。多用于SELECT查询数据
ExecuteNonQuery
对Connection 执行 SQL 语句,并返回受影响的行数(int),多用于INSERT、UPDATE、DELETE、CREATE等操作
ExecuteScalar
返回单个值。返回结果集中第一行的第一列。忽略额外的列或行
ExecuteXmlReader
将 CommandText 发送到 Connection 并生成一个 XmlReader 对象
例如:
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString);
- SqlCommand cmd = myConnection.CreateCommand();
- cmd.CommandText = "SELECT * FROM P_Product";
- myConnection.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- //SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- while (dr.Read())//循环读取数据
- {
- Response.Write(dr.GetInt32(0).ToString() + ", " + dr.GetString(1) +
- "<br>");
- }
- dr.Close();
- myConnection.Close();
- string conString = "data source=127.0.0.1;Database=codematic;user id=sa;
- password=";
- SqlConnection myConnection = new SqlConnection(conString);
- string strSql = "select count(*) from P_Product";
- SqlCommand myCommand = new SqlCommand(strSql, myConnection);
- myConnection.Open();
- int count = (int)myCommand.ExecuteScalar();
- myConnection.Close();
- Response.Write(count);
很多数据库支持将多条命令合并或批处理成一条单一命令执行。例如,SQL Server使你可以用分号";"分隔命令。将多条命令合并成单一命令,能减少到服务器的行程数,并提高应用程序的性能。例如,可以将所有预定的删除在应用程序中本地存储起来,然后再发出一条批处理命令调用,从数据源删除它们。
虽然这样做确实能提高性能,但是当对DataSet中的数据更新进行管理时,可能会增加应用程序的复杂性。要保持简单,需要在DataSet中为每个DataTable创建一个DataAdapter。
注 意 使用SqlCommand执行存储过程的快速提示:如果调用存储过程,则需将SqlCommand的CommandType属性指定为StoredProcedure。这样在将该命令显式标识为存储过程时,就不需要在执行之前分析命令了。
- Command
- Command
- Command
- command
- Command
- Command
- command
- command!!!
- command
- command
- Command
- Command
- Command
- Command
- command
- Command:
- Command
- Command :
- ADO.NET中主要对象
- OpenInventor试图显示SoWinRenderArea
- Mysql中取字符串单字节ascii码值处理
- 内部类代码实例
- iOS软件开发揭密:iPhone&iPad企业应用和游戏开发(china-pub首发)
- Command
- 转型IT讲师,挑战年薪30万
- 用方括号填充向量
- Android2.2快速入门(1)
- android画图--切割区域
- oracle读取数据错误和hibernate几个问题
- View.invalidate()的调用顺序
- jQuery 二级联动
- 天真热