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(); }
例如: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。
本文出自《亮剑.NET. .NET深入体验与实战精要》一书
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 对象 |
[align=center]注 意[/align] | 使用SqlCommand执行存储过程的快速提示:如果调用存储过程,则需将SqlCommand的CommandType属性指定为StoredProcedure。这样在将该命令显式标识为存储过程时,就不需要在执行之前分析命令了。 |
相关文章推荐
- ADO.NET与抽水的故事 系列三——抽水机—Command
- ADO.NET与抽水的故事 系列六——水池子——DataTable
- ADO.NET与抽水的故事 系列五——水库管理——DataSet
- ADO.NET与抽水的故事 系列四——输水管——DataAdapter
- ADO.NET与抽水的故事 系列六——水池子——DataTable
- ADO.NET与抽水的故事 系列五——水库管理——DataSet
- ADO.NET与抽水的故事 系列四——输水管——DataAdapter
- Ado.NET 系列quiz
- C# WinForm开发系列 - ADO.NET
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #3 -- LINQ-to-SQL、EntitySQL、查询产生器方法(Query builder)三种语法
- Quartz.NET开源作业调度框架系列(五):AdoJobStore保存job到数据库
- ADO.NET_第七篇_OracleCommand_03ExecuteScalar
- .Net Core 系列:2、ADO.Net 基础
- ADO.NET之command更新数据(视图版)
- 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services
- ADO.NET Insert command function
- ADO.NET笔记——使用Command执行增删改操作,通过判断ExecuteNonQuery()返回值检查是否操作成功
- 27:ASP.NET复习系列- ADO.NET基本知识点-数据集的使用方法和技巧
- 34:ASP.NET复习系列- ADO.NET提高-深入了解DataSet
- ADO.NET之command查询数据