日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
合理使用ADO.NET參數(shù)

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

創(chuàng)新互聯(lián)10多年企業(yè)網站設計服務;為您提供網站建設,網站制作,網頁設計及高端網站定制服務,企業(yè)網站設計及推廣,對PE包裝袋等多個行業(yè)擁有多年的網站設計經驗的網站建設公司。

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

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

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

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

Visual Basic

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

分享標題:合理使用ADO.NET參數(shù)
文章轉載:http://www.5511xx.com/article/cochgpi.html