您的位置:首页 > 职场人生

ADO.NET与抽水的故事 系列三——抽水机—Command

2009-06-16 12:44 645 查看
趣味理解ADO.NET对象模型抽水机——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。

[align=center]注 意[/align]
使用SqlCommand执行存储过程的快速提示:如果调用存储过程,则需将SqlCommand的CommandType属性指定为StoredProcedure。这样在将该命令显式标识为存储过程时,就不需要在执行之前分析命令了。
本文出自《亮剑.NET. .NET深入体验与实战精要》一书

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 休闲 Command