ADO.NET在开发中的部分使用方法和技巧 [转]

[ 3794 查看 / 10 回复 ]

使用 SqlAdapter 生成 DataSet 或 DataTable <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.创建一个 SqlCommand 对象以调用该存储过程,并将其与一个 SqlConnection 对象(显示)或连接字符串(不显示)相关联。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.创建一个新的 SqlDataAdapter 对象并将其与 SqlCommand 对象相关联。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.创建一个 DataTable(也可以创建一个 DataSet)对象。使用构造函数参数来命名 DataTable。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.调用 SqlDataAdapter 对象的 Fill 方法,用检索到的行填充 DataSet 或 DataTable。
  1. using System.Data;
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using System.Data.SqlClient;
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. public DataTable RetrieveRowsWithDataTable() {
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. using ( SqlConnection conn = new SqlConnection(connectionString) ) {
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. conn.Open();
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. SqlCommand cmd = new SqlCommand("DATRetrieveProducts", conn);
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. cmd.CommandType = CommandType.StoredProcedure;
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. SqlDataAdapter adapter = new SqlDataAdapter( cmd );
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. DataTable dataTable = new DataTable("Products");
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. adapter .Fill(dataTable);
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. return dataTable; }
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  23. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 SqlDataReader 来检索多个行

以下代码片段阐明了可检索多个行的 SqlDataReader方法。
  1. using System.IO;
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using System.Data;
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. using System.Data.SqlClient;
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. public SqlDataReader RetrieveRowsWithDataReader()  {
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind");
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. SqlCommand cmd = new SqlCommand("DATRetrieveProducts", conn );
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. cmd.CommandType = CommandType.StoredProcedure;
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. try { conn.Open();
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. // Generate the reader. CommandBehavior.CloseConnection causes the
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. // the connection to be closed when the reader object is closed
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. return( cmd.ExecuteReader( CommandBehavior.CloseConnection ) ); }
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23. catch { conn.Close(); throw; }
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25. }
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27. // Display the product list using the console
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29. private void DisplayProducts() {
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31. SqlDataReader reader = RetrieveRowsWithDataReader();
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33. try { while (reader.Read()) {
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35. Console.WriteLine("{0} {1} {2}", reader.GetInt32(0).ToString(), reader.GetString(1) ); }
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37. }
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39. finally {
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41. reader.Close();
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  43. // Also closes the connection due to the
  44. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  45. // CommandBehavior enum used when generating the reader }
  46. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  47. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用 SqlDataReader 检索行 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.创建一个用来执行存储过程的 SqlCommand 对象,并将其与一个 SqlConnection对象相关联。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.通过调用 SqlCommand 对象的 ExecuteReader方法创建一个 SqlDataReader对象。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.要从流中读取数据,请调用 SqlDataReader对象的 Read方法来检索行,并使用类型化访问器方法(如 GetInt32和 GetString方法)来检索列值。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
5.使用完读取器后,请调用其 Close方法。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 XmlReader 检索多个行

可以使用 SqlCommand对象来生成 XmlReader对象,后者可提供对 XML 数据的基于流的只进访问。命令(通常为存储过程)必须产生基于 XML 的结果集,对于 SQL Server 2000 而言,该结果集通常包含一个带有有效 FOR XML子句的 SELECT语句。以下代码片段阐明了该方法:
  1. public void RetrieveAndDisplayRowsWithXmlReader() {
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection(connectionString) ) {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. SqlCommand cmd = new SqlCommand("DATRetrieveProductsXML", conn );
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmd.CommandType = CommandType.StoredProcedure;
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. try { conn.Open();
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. XmlTextReader xreader = (XmlTextReader)cmd.ExecuteXmlReader();
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. while ( xreader.Read() ) {
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. if ( xreader.Name == "PRODUCTS" ) {
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. string strOutput = xreader.GetAttribute("ProductID");
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. strOutput += " "; strOutput += xreader.GetAttribute("ProductName");
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. Console.WriteLine( strOutput ); }
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23. }
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25. xreader.Close();
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27. // XmlTextReader does not support IDisposable so it can't be
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29. // used within a using keyword
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31. }
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  33. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
上述代码使用了以下存储过程:<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE DATRetrieveProductsXML AS SELECT * FROM PRODUCTS FOR XML AUTO GO <ŠP”/*Ëi£°nfhot.comvO{' ÐK
使用 XmlReader 检索 XML 数据 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.创建一个 SqlCommand 对象来调用可生成 XML 结果集的存储过程(例如,在 SELECT语句中使用 FOR XML子句)。将该 SqlCommand对象与某个连接相关联。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.调用 SqlCommand 对象的 ExecuteXmlReader方法,并且将结果分配给只进 XmlTextReader对象。当您不需要对返回的数据进行任何基于 XML 的验证时,这是应该使用的最快类型的 XmlReader对象。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.使用 XmlTextReader 对象的 Read方法来读取数据。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用存储过程输出参数来检索单个行

借助于命名的输出参数,可以调用在单个行内返回检索到的数据项的存储过程。以下代码片段使用存储过程来检索 Northwind 数据库的 Products 表中包含的特定产品的产品名称和单价。
  1. void GetProductDetails( int ProductID, out string ProductName, out decimal UnitPrice ) {
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=Northwind") ){
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. // Set up the command object used to execute the stored proc
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. SqlCommand cmd = new SqlCommand( "DATGetProductDetailsSPOutput", conn )
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. cmd.CommandType = CommandType.StoredProcedure;
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. // Establish stored proc parameters.
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. // @ProductID int INPUT
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. // @ProductName nvarchar(40) OUTPUT
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. // @UnitPrice money OUTPUT
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. // Must explicitly set the direction of output parameters SqlParameter paramProdID = cmd.Parameters.Add( "@ProductID", ProductID );
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. paramProdID.Direction = ParameterDirection.Input; SqlParameter paramProdName = cmd.Parameters.Add( "@ProductName", SqlDbType.VarChar, 40 );
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23. paramProdName.Direction = ParameterDirection.Output; SqlParameter paramUnitPrice = cmd.Parameters.Add( "@UnitPrice", SqlDbType.Money );
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25. paramUnitPrice.Direction = ParameterDirection.Output; conn.Open();
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27. // Use ExecuteNonQuery to run the command.
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29. // Although no rows are returned any mapped output parameters
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31. // (and potentially return values) are populated cmd.ExecuteNonQuery( );
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33. // Return output parameters from stored proc ProductName = paramProdName.Value.ToString();
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35. UnitPrice = (decimal)paramUnitPrice.Value; } }
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用存储过程输出参数来检索单个行 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.创建一个 SqlCommand 对象并将其与一个 SqlConnection对象相关联。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.通过调用 SqlCommand 的 Parameters集合的 Add方法来设置存储过程参数。默认情况下,参数都被假设为输入参数,因此必须显式设置任何输出参数的方向。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
注:一种良好的习惯做法是显式设置所有参数(包括输入参数)的方向。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.调用 SqlCommand 对象的 ExecuteNonQuery方法。这将填充输出参数(并可能填充返回值)。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
5.通过使用 Value 属性,从适当的 SqlParameter对象中检索输出参数。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
6.关闭连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上述代码片段调用了以下存储过程。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE DATGetProductDetailsSPOutput <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID int,<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductName nvarchar(40) OUTPUT, <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@UnitPrice money OUTPUT AS SELECT @ProductName = ProductName, <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@UnitPrice = UnitPrice FROM Products WHERE ProductID = @ProductID GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 SqlDataReader 来检索单个行

可以使用 SqlDataReader对象来检索单个行,尤其是可以从返回的数据流中检索需要的列值。以下代码片段对此进行了说明。
  1. void GetProductDetailsUsingReader( int ProductID, out string ProductName, out decimal UnitPrice ){
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=Northwind") ) {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. // Set up the command object used to execute the stored proc
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. SqlCommand cmd = new SqlCommand( "DATGetProductDetailsReader", conn );
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. cmd.CommandType = CommandType.StoredProcedure;
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. // Establish stored proc parameters.
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. // @ProductID int INPUT
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. SqlParameter paramProdID = cmd.Parameters.Add( "@ProductID", ProductID );
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. paramProdID.Direction = ParameterDirection.Input;
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. conn.Open();
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. using( SqlDataReader reader = cmd.ExecuteReader() ) { if( reader.Read() )
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23. // Advance to the one and only row {
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25. // Return output parameters from returned data stream
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27. ProductName = reader.GetString(0);
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29. UnitPrice = reader.GetDecimal(1);
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31. } } } } 
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用 SqlDataReader 对象来返回单个行 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.建立 SqlCommand 对象。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.调用 SqlDataReader 对象的 ExecuteReader方法。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.通过 SqlDataReader 对象的类型化访问器方法(在这里,为 GetString和 GetDecimal)来检索输出参数。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上述代码片段调用了以下存储过程。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE DATGetProductDetailsReader <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID int AS SELECT ProductName, UnitPrice FROM Products WHERE ProductID =<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 ExecuteScalar 来检索单个项

ExecuteScalar方法专门适用于仅返回单个值的查询。如果查询返回多个列和/或行,ExecuteScalar将只返回第一行的第一列。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
以下代码说明了如何查找与特定产品 ID 相对应的产品名称:
  1. void GetProductNameExecuteScalar( int ProductID, out string ProductName ){
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") )
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. { SqlCommand cmd = new SqlCommand("LookupProductNameScalar", conn );
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ProductID", ProductID );
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. conn.Open();
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. ProductName = (string)cmd.ExecuteScalar();
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. } }
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用 ExecuteScalar 来检索单个项 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.建立一个 SqlCommand 对象来调用存储过程。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.调用 ExecuteScalar 方法。注意,该方法返回一个对象类型。它包含检索到的第一列的值,并且必须转化为适当的类型。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.关闭连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上述代码使用了以下存储过程:<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE LookupProductNameScalar<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID int AS SELECT TOP 1 ProductName FROM Products WHERE ProductID = <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用存储过程输出或返回参数来检索单个项

可以使用存储过程输出或返回参数来查找单个值。以下代码阐明了输出参数的用法:
  1. void GetProductNameUsingSPOutput( int ProductID, out string ProductName ) {
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") )
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. { SqlCommand cmd = new SqlCommand("LookupProductNameSPOutput", conn );
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmd.CommandType = CommandType.StoredProcedure;
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. SqlParameter paramProdID = cmd.Parameters.Add("@ProductID", ProductID );
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. ParamProdID.Direction = ParameterDirection.Input;
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. SqlParameter paramPN = cmd.Parameters.Add("@ProductName", SqlDbType.VarChar, 40 );
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. paramPN.Direction = ParameterDirection.Output;
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. conn.Open();
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. cmd.ExecuteNonQuery();
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. ProductName = paramPN.Value.ToString();
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.                
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25.                
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27.                        
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  29. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31.                         } }
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用存储过程输出参数来检索单个值 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.建立一个 SqlCommand 对象来调用存储过程。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.通过将 SqlParameters 添加到 SqlCommand的 Parameters集合中,设置任何输入参数和单个输出参数。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.调用 SqlCommand 对象的 ExecuteNonQuery方法。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
5.关闭连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
6.通过使用输出 SqlParameter 的 Value属性来检索输出值。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上述代码使用了以下存储过程。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID int,<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductName nvarchar(40) OUTPUT AS SELECT <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductName = ProductName FROM Products WHERE ProductID = @ProductID GO <ŠP”/*Ëi£°nfhot.comvO{' ÐK
以下代码阐明了如何使用返回值来指明是否存在特定行。从编码角度来看,这类似于使用存储过程输出参数,不同之处在于必须将 SqlParameter方向显式设置为 ParameterDirection.ReturnValue。
  1. bool CheckProduct( int ProductID ) {
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") ){
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. SqlCommand cmd = new SqlCommand("CheckProductSP", conn );
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmd.CommandType = CommandType.StoredProcedure;
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. cmd.Parameters.Add("@ProductID", ProductID );
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. SqlParameter paramRet = cmd.Parameters.Add("@ProductExists", SqlDbType.Int );
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. paramRet.Direction = ParameterDirection.ReturnValue; conn.Open(); cmd.ExecuteNonQuery(); }
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. return (int)paramRet.Value == 1; } 
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
通过使用存储过程返回值来检查是否存在特定行 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.建立一个 SqlCommand 对象来调用存储过程。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.设置一个输入参数,该参数含有要访问的行的主键值。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.设置单个返回值参数。将一个 SqlParameter 对象添加到 SqlCommand的 Parameters集合中,并将其方向设置为 ParameterDirection.ReturnValue。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
4.打开连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
5.调用 SqlCommand 对象的 ExecuteNonQuery方法。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
6.关闭连接。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
7.通过使用返回值 SqlParameter 的 Value属性来检索返回值。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上述代码使用了以下存储过程。 <ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE CheckProductSP <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID int AS IF EXISTS( SELECT ProductID FROM Products WHERE ProductID = <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ProductID ) <ŠP”/*Ëi£°nfhot.comvO{' ÐK
return 1 ELSE return 0 GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 SqlDataReader 来检索单个项

可以使用 SqlDataReader对象并通过调用命令对象的 ExecuteReader方法来获取单个输出值。这要求编写稍微多一点的代码,因为必须调用 SqlDataReader Read 方法,然后通过该读取器的访问器方法之一来检索需要的值。以下代码阐明了 SqlDataReader对象的用法。
  1. bool CheckProductWithReader( int ProductID ) {
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. using( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=northwind") )
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. { SqlCommand cmd = new SqlCommand("CheckProductExistsWithCount", conn );
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmd.CommandType = CommandType.StoredProcedure;
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. cmd.Parameters.Add("@ProductID", ProductID );
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. cmd.Parameters["@ProductID"].Direction = ParameterDirection.Input; conn.Open();
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. using( SqlDataReader reader = cmd.ExecuteReader( CommandBehavior.SingleResult ) )
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. { if( reader.Read() ) { return (reader.GetInt32(0) >0); } return false; } } 
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
上述代码采用了以下存储过程。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE CheckProductExistsWithCount @ProductID int AS SELECT COUNT(*) FROM Products WHERE ProductID = @ProductID GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何编写 ADO.NET 手动事务处理代码

以下代码显示了如何充分利用 SQL Server .NET 数据提供程序所提供的事务处理支持,通过事务来保护资金转帐操作。该操作在同一数据库中的两个帐户之间转移资金。
  1. public void TransferMoney( string toAccount, string fromAccount, decimal amount ) {  using ( SqlConnection conn = new SqlConnection( "server=(local);Integrated Security=SSPI;database=SimpleBank" ) )
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. { SqlCommand cmdCredit = new SqlCommand("Credit", conn );
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5. cmdCredit.CommandType = CommandType.StoredProcedure;
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. cmdCredit.Parameters.Add( new SqlParameter("@AccountNo", toAccount) );
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9. cmdCredit.Parameters.Add( new SqlParameter("@Amount", amount ));
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11. SqlCommand cmdDebit = new SqlCommand("Debit", conn );
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13. cmdDebit.CommandType = CommandType.StoredProcedure; cmdDebit.Parameters.Add( new SqlParameter("@AccountNo", fromAccount) );
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15. cmdDebit.Parameters.Add( new SqlParameter("@Amount", amount ));
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17. conn.Open();
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19. // Start a new transaction using ( SqlTransaction trans = conn.BeginTransaction() ) {
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21. // Associate the two command objects with the same transaction cmdCredit.Transaction = trans;
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23. cmdDebit.Transaction = trans;
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25. try { cmdCredit.ExecuteNonQuery();
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27. cmdDebit.ExecuteNonQuery(); // Both commands (credit and debit) were successful trans.Commit(); }
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29. catch( Exception ex ) {
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31. // transaction failed trans.Rollback();
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33. // log exception details . . .
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35. throw ex; } } } } 
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP

如何使用 Transact-SQL 执行事务处理

以下存储过程阐明了如何在 Transact-SQL 存储过程内部执行事务性资金转帐操作。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
CREATE PROCEDURE MoneyTransfer <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@FromAccount char(20),<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@ToAccount char(20), <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@Amount money AS BEGIN TRANSACTION -- PERFORM DEBIT OPERATION UPDATE Accounts SET Balance = Balance -<ŠP”/*Ëi£°nfhot.comvO{' ÐK
@Amount WHERE AccountNumber = @FromAccount IF RowCount = 0 BEGIN RAISERROR('Invalid From Account Number', 11, 1) <ŠP”/*Ëi£°nfhot.comvO{' ÐK
GOTO ABORT END DECLARE <ŠP”/*Ëi£°nfhot.comvO{' ÐK
@Balance money SELECT @Balance = Balance FROM ACCOUNTS <ŠP”/*Ëi£°nfhot.comvO{' ÐK
WHERE AccountNumber = @FromAccount IF @BALANCE <ŠP”/*Ëi£°nfhot.comvO{' ÐK
0 BEGIN RAISERROR('Insufficient funds', 11, 1)<ŠP”/*Ëi£°nfhot.comvO{' ÐK
GOTO ABORT END -- PERFORM CREDIT OPERATION UPDATE Accounts SET Balance = Balance + @Amount WHERE AccountNumber = @ToAccount <ŠP”/*Ëi£°nfhot.comvO{' ÐK
IF RowCount = 0 BEGIN RAISERROR('Invalid To Account Number', 11, 1) GOTO ABORT END COMMIT TRANSACTION RETURN 0 ABORT: ROLLBACK TRANSACTION GO<ŠP”/*Ëi£°nfhot.comvO{' ÐK
该存储过程使用 BEGIN TRANSACTION、COMMIT TRANSACTION 和 ROLLBACK TRANSACTION 语句来手动控制该事务。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP