溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用ADO.NET參數(shù)

發(fā)布時(shí)間:2021-11-03 15:08:09 來(lái)源:億速云 閱讀:164 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)如何使用ADO.NET參數(shù),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在數(shù)據(jù)驅(qū)動(dòng)的應(yīng)用程序中,存儲(chǔ)過(guò)程具有許多優(yōu)勢(shì)。通過(guò)利用存儲(chǔ)過(guò)程,數(shù)據(jù)庫(kù)操作可以封裝在單個(gè)命令中,為獲取***性能而進(jìn)行優(yōu)化并通過(guò)附加的安全性得到增強(qiáng)。盡管可以通過(guò)在 SQL 語(yǔ)句中傳遞后接參數(shù)自變量的存儲(chǔ)過(guò)程名稱(chēng)來(lái)調(diào)用相應(yīng)的存儲(chǔ)過(guò)程,但如果使用 ADO.NET DbCommand 對(duì)象的 Parameters 集合,則可以讓您更為明確地定義存儲(chǔ)過(guò)程參數(shù),并訪(fǎng)問(wèn)輸出參數(shù)和返回值。

使用ADO.NET參數(shù)化語(yǔ)句在服務(wù)器上通過(guò)使用 sp_executesql 執(zhí)行,sp_executesql 允許重復(fù)使用查詢(xún)計(jì)劃。sp_executesql 批處理命令中的本地光標(biāo)或變量對(duì)于調(diào)用 sp_executesql 的批處理命令是不可見(jiàn)的。數(shù)據(jù)庫(kù)上下文中的更改只持續(xù)到 sp_executesql 語(yǔ)句的結(jié)尾。

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

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

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

Visual Basic

Shared Sub GetSalesByCategory(ByVal connectionString As String, _  ByVal categoryName As String)   Using connection As New SqlConnection(connectionString)   ' Create the command and set its properties.  Dim command As SqlCommand = New SqlCommand()  command.Connection = connection command.CommandText = "SalesByCategory" command.CommandType = CommandType.StoredProcedure   ' Add the input parameter and set its properties.  Dim parameter As New SqlParameter()  parameter.ParameterName = "@CategoryName" parameter.SqlDbType = SqlDbType.NVarChar  parameter.Direction = ParameterDirection.Input  parameter.Value = categoryName  ' Add the parameter to the Parameters collection.  command.Parameters.Add(parameter)   ' Open the connection and execute the reader.  connection.Open()  Dim reader As SqlDataReader = command.ExecuteReader()   If reader.HasRows Then  Do While reader.Read()  Console.WriteLine("{0}: {1:C}", _  reader(0), reader(1))  Loop  Else  Console.WriteLine("No rows returned.")  End If  End Using  End Sub

C#

static void GetSalesByCategory(string connectionString,   string categoryName)  {  using (SqlConnection connection = new SqlConnection(connectionString))  {  // Create the command and set its properties.  SqlCommand command = new SqlCommand();  command.Connection = connection;  command.CommandText = "SalesByCategory";  command.CommandType = CommandType.StoredProcedure;   // Add the input parameter and set its properties.  SqlParameter parameter = new SqlParameter();  parameter.ParameterName = "@CategoryName";  parameter.SqlDbType = SqlDbType.NVarChar;  parameter.Direction = ParameterDirection.Input;  parameter.Value = categoryName;   // Add the parameter to the Parameters collection.   command.Parameters.Add(parameter);   // Open the connection and execute the reader.  connection.Open();  SqlDataReader reader = command.ExecuteReader();   if (reader.HasRows)  {  while (reader.Read())  {  Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);  }  }  else  {  Console.WriteLine("No rows found.");  }  reader.Close();  }  }

關(guān)于“如何使用ADO.NET參數(shù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI