Command

来源:互联网 发布:java 防止盗刷短信 编辑:程序博客网 时间:2024/05/21 06:20

5.1.4  抽水机--Command

Command对象封装了与用户想要完成的动作相关的数据库命令。在一般情况下这些命令就是SQL语句。

1.构造SqlCommand

(1)初始化SqlCommand类的新实例。

  1. string conString = "data source=127.0.0.1;Database=codematic;user id=sa;  
  2.         password=";  
  3. SqlConnection myConnection = new SqlConnection(conString);  
  4.               
  5. SqlCommand myCommand = new SqlCommand();  
  6. myCommand.Connection = myConnection;  
  7. myCommand.CommandText = "update P_Product set Name='电脑1' where Id=52";  
  8. myConnection.Open();  
  9. int rows = myCommand.ExecuteNonQuery();  
  10. myConnection.Close(); 

(2)初始化具有查询文本的 SqlCommand 类的新实例。

  1. string conString  = "data source=127.0.0.1;Database=codematic;user id=sa;  
  2.         password=";  
  3. SqlConnection myConnection = new SqlConnection(conString );  
  4. string strSql = "update P_Product set Name='电脑2' where Id=52";  
  5. SqlCommand myCommand = new SqlCommand(strSql);  
  6. myCommand.Connection = myConnection;             
  7. myConnection.Open();  
  8. int rows = myCommand.ExecuteNonQuery();  
  9. myConnection.Close(); 

(3)初始化具有查询文本和 SqlConnection 的SqlCommand类实例。

  1. string conString  = "data source=127.0.0.1;Database=codematic;user id=sa;  
  2.         password=";  
  3. SqlConnection myConnection = new SqlConnection(conString );  
  4. string strSql = "update P_Product set Name='电脑3' where Id=52";  
  5. SqlCommand myCommand = new SqlCommand(strSql, myConnection);  
  6. myConnection.Open();  
  7. int rows = myCommand.ExecuteNonQuery();  
  8. myConnection.Close(); 

(4)初始化具有查询文本、SqlConnection 和 Transaction 的 SqlCommand 类实例。

  1. string conString  = "data source=127.0.0.1;Database=codematic;user id=sa;  
  2.         password=";  
  3. SqlConnection myConnection = new SqlConnection(conString );  
  4. string strSql = "update P_Product set Name='电脑4' where Id=52";  
  5. string strSql2 = "update P_Product set Name='数码4' where Id=53";  
  6. myConnection.Open();  
  7. SqlTransaction myTrans = myConnection.BeginTransaction();  
  8. SqlCommand myCommand = new SqlCommand(strSql, myConnection, myTrans);  
  9. try 
  10. {  
  11.     int rows = myCommand.ExecuteNonQuery();  
  12.     myCommand.CommandText = strSql2;  
  13.     rows = myCommand.ExecuteNonQuery();  
  14.     myTrans.Commit();  
  15.     myConnection.Close();  
  16. }  
  17. catch 
  18. {  
  19.     myTrans.Rollback();                  

2.建立SqlCommand与SqlConnection的关联

  1. myCommand.Connection = myConnection; 

//或者

  1. SqlCommand myCommand = myConnection.CreateCommand; 

3.设置SqlCommand的查询文本

  1. myCommand.CommandText = "SELECT * FROM P_Product ";  
  2. //或者第2种构造:  
  3. SqlCommand myCommand = new SqlCommand(strSql); 

4.执行命令

SqlCommand方法如表5-1所示。

表5-1  SqlCommand方法

    

    

ExecuteReader

返回一行或多行。多用于SELECT查询数据

ExecuteNonQuery

Connection 执行 SQL 语句,并返回受影响的行数(int),多用于INSERTUPDATEDELETECREATE等操作

ExecuteScalar

返回单个值。返回结果集中第一行的第一列。忽略额外的列或行

ExecuteXmlReader

 CommandText 发送到 Connection 并生成一个 XmlReader 对象

例如:

  1. string conString = "data source=127.0.0.1;Database=codematic;user id=sa;  
  2.         password=";  
  3. SqlConnection myConnection = new SqlConnection(conString);  
  4. SqlCommand cmd = myConnection.CreateCommand();  
  5. cmd.CommandText = "SELECT * FROM P_Product";  
  6. myConnection.Open();  
  7. SqlDataReader dr = cmd.ExecuteReader();  
  8. //SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
  9. while (dr.Read())//循环读取数据  
  10. {  
  11.     Response.Write(dr.GetInt32(0).ToString() + ", " + dr.GetString(1) +   
  12.             "<br>");  
  13. }  
  14. dr.Close();  
  15. myConnection.Close();  
  16.  
  17. string conString = "data source=127.0.0.1;Database=codematic;user id=sa;  
  18.     password=";  
  19. SqlConnection myConnection = new SqlConnection(conString);  
  20. string strSql = "select count(*) from P_Product";  
  21. SqlCommand myCommand = new SqlCommand(strSql, myConnection);  
  22. myConnection.Open();  
  23. int count = (int)myCommand.ExecuteScalar();  
  24. myConnection.Close();  
  25. Response.Write(count); 

很多数据库支持将多条命令合并或批处理成一条单一命令执行。例如,SQL Server使你可以用分号";"分隔命令。将多条命令合并成单一命令,能减少到服务器的行程数,并提高应用程序的性能。例如,可以将所有预定的删除在应用程序中本地存储起来,然后再发出一条批处理命令调用,从数据源删除它们。

虽然这样做确实能提高性能,但是当对DataSet中的数据更新进行管理时,可能会增加应用程序的复杂性。要保持简单,需要在DataSet中为每个DataTable创建一个DataAdapter。

注  意 使用SqlCommand执行存储过程的快速提示:如果调用存储过程,则需将SqlCommand的CommandType属性指定为StoredProcedure。这样在将该命令显式标识为存储过程时,就不需要在执行之前分析命令了。