自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

合理使用ADO.NET參數(shù)

開發(fā) 后端
這里就使用ADO.NET參數(shù)如何調(diào)用 Northwind 示例數(shù)據(jù)庫中的 SQL Server 存儲過程做出了詳細的介紹,希望對大家有幫助。

使用ADO.NET參數(shù)還是比較常用的,于是我研究了一下對 SqlCommand 和存儲過程使用參數(shù),在這里拿出來和大家分享一下,希望對大家有用。在數(shù)據(jù)驅(qū)動的應(yīng)用程序中,存儲過程具有許多優(yōu)勢。通過利用存儲過程,數(shù)據(jù)庫操作可以封裝在單個命令中,為獲取***性能而進行優(yōu)化并通過附加的安全性得到增強。盡管可以通過在 SQL 語句中傳遞后接參數(shù)自變量的存儲過程名稱來調(diào)用相應(yīng)的存儲過程,但如果使用 ADO.NET DbCommand 對象的 Parameters 集合,則可以讓您更為明確地定義存儲過程參數(shù),并訪問輸出參數(shù)和返回值。

使用ADO.NET參數(shù)化語句在服務(wù)器上通過使用 sp_executesql 執(zhí)行,sp_executesql 允許重復(fù)使用查詢計劃。sp_executesql 批處理命令中的本地光標(biāo)或變量對于調(diào)用 sp_executesql 的批處理命令是不可見的。數(shù)據(jù)庫上下文中的更改只持續(xù)到 sp_executesql 語句的結(jié)尾。有關(guān)更多信息,請參見 SQL Server 聯(lián)機叢書。

#T#對 SqlCommand 使用參數(shù)以執(zhí)行 SQL Server 存儲過程時,添加到 Parameters 集合中的參數(shù)的名稱必須與存儲過程中參數(shù)標(biāo)記的名稱相匹配。SQL Server 的 .NET Framework 數(shù)據(jù)訪問接口不支持問號 (?)使用ADO.NET參數(shù)傳遞到 SQL 語句或存儲過程的占位符。它將存儲過程中的參數(shù)視為命名參數(shù),并搜索匹配的參數(shù)標(biāo)記。例如,通過使用名為 @CustomerID 的參數(shù)定義 CustOrderHist 存儲過程。您的代碼在執(zhí)行該存儲過程時,它也必須使用名為 @CustomerID 的參數(shù)。

此示例演示了如何調(diào)用 Northwind 示例數(shù)據(jù)庫中的 SQL Server 存儲過程。存儲過程的名稱為 dbo.SalesByCategory,它具有名為 @CategoryName 的輸入?yún)?shù),其數(shù)據(jù)類型為 nvarchar(15)。該代碼在 using 代碼塊內(nèi)創(chuàng)建一個新 SqlConnection,以便在過程結(jié)束時釋放連接。會創(chuàng)建 SqlCommand 和 SqlParameter 對象,并設(shè)置其屬性。SqlDataReader 會執(zhí)行 SqlCommand 并從存儲過程返回結(jié)果集,以在控制臺窗口中顯示相關(guān)輸出。

您可以選擇使用任一重載構(gòu)造函數(shù)在一個語句中設(shè)置多個屬性,而不是創(chuàng)建 SqlCommand 和 SqlParameter 對象,然后在各個語句中設(shè)置屬性。

Visual Basic

  1. Shared Sub GetSalesByCategory(ByVal connectionString As String, _  
  2. ByVal categoryName As String)  
  3.  
  4. Using connection As New SqlConnection(connectionString)  
  5.  
  6. ' Create the command and set its properties.  
  7. Dim command As SqlCommand = New SqlCommand()  
  8. command.Connection = connection 
  9. command.CommandText = "SalesByCategory" 
  10. command.CommandType = CommandType.StoredProcedure  
  11.  
  12. ' Add the input parameter and set its properties.  
  13. Dim parameter As New SqlParameter()  
  14. parameter.ParameterName = "@CategoryName" 
  15. parameter.SqlDbType = SqlDbType.NVarChar  
  16. parameter.Direction = ParameterDirection.Input  
  17. parameter.Value = categoryName 
  18.  
  19. ' Add the parameter to the Parameters collection.  
  20. command.Parameters.Add(parameter)  
  21.  
  22. ' Open the connection and execute the reader.  
  23. connection.Open()  
  24. Dim reader As SqlDataReader = command.ExecuteReader()  
  25.  
  26. If reader.HasRows Then  
  27. Do While reader.Read()  
  28. Console.WriteLine("{0}: {1:C}", _  
  29. reader(0), reader(1))  
  30. Loop  
  31. Else  
  32. Console.WriteLine("No rows returned.")  
  33. End If  
  34. End Using  
  35. End Sub  

C#

  1. static void GetSalesByCategory(string connectionString,   
  2. string categoryName)  
  3. {  
  4. using (SqlConnection connection = new SqlConnection(connectionString))  
  5. {  
  6. // Create the command and set its properties.  
  7. SqlCommand command = new SqlCommand();  
  8. command.Connection = connection;  
  9. command.CommandText = "SalesByCategory";  
  10. command.CommandType = CommandType.StoredProcedure;  
  11.  
  12. // Add the input parameter and set its properties.  
  13. SqlParameter parameter = new SqlParameter();  
  14. parameter.ParameterName = "@CategoryName";  
  15. parameter.SqlDbType = SqlDbType.NVarChar;  
  16. parameter.Direction = ParameterDirection.Input;  
  17. parameter.Value = categoryName;  
  18.  
  19. // Add the parameter to the Parameters collection.   
  20. command.Parameters.Add(parameter);  
  21.  
  22. // Open the connection and execute the reader.  
  23. connection.Open();  
  24. SqlDataReader reader = command.ExecuteReader();  
  25.  
  26. if (reader.HasRows)  
  27. {  
  28. while (reader.Read())  
  29. {  
  30. Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);  
  31. }  
  32. }  
  33. else  
  34. {  
  35. Console.WriteLine("No rows found.");  
  36. }  
  37. reader.Close();  
  38. }  
  39. }  
責(zé)任編輯:田樹 來源: 博客
相關(guān)推薦

2009-11-13 10:57:28

ADO.NET Dat

2009-12-28 15:11:36

ADO.NET專家

2009-12-21 14:04:48

ADO.NET參數(shù)

2009-10-29 10:34:31

ADO.NET使用技巧

2009-11-12 10:15:37

ADO.NET使用

2009-11-04 09:18:12

ADO.NET _Re

2009-11-04 10:07:52

ADO.NET DbP

2009-12-22 16:35:11

ADO.NET控件

2009-12-22 15:20:25

ADO.NET功能

2009-12-21 14:50:47

ADO.NET優(yōu)化

2009-08-21 16:35:08

使用C#結(jié)合ADO.N

2009-12-21 16:53:06

ADO.NET使用說明

2009-12-31 13:50:46

ADO.NET模型

2009-12-29 10:36:24

ADO.NET 工具

2009-12-30 14:21:21

ADO.NET設(shè)置

2009-12-25 16:56:36

ADO.NET建立連接

2009-12-31 14:28:09

ADO.NET參數(shù)

2009-11-13 14:22:11

ADO.NET Dat

2009-11-13 14:38:45

ADO.NET Dat

2009-11-13 10:10:07

點贊
收藏

51CTO技術(shù)棧公眾號