久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

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

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

創(chuàng)新互聯(lián)致力于成都做網(wǎng)站、網(wǎng)站設(shè)計,成都網(wǎng)站設(shè)計,集團網(wǎng)站建設(shè)等服務標準化,推過標準化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務水平進行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場競爭中脫穎而出。 選擇創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務!

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

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

對 SqlCommand 使用參數(shù)以執(zhí)行 SQL Server 存儲過程時,添加到 Parameters 集合中的參數(shù)的名稱必須與存儲過程中參數(shù)標記的名稱相匹配。SQL Server 的 .NET Framework 數(shù)據(jù)訪問接口不支持問號 (?)使用ADO.NET參數(shù)傳遞到 SQL 語句或存儲過程的占位符。它將存儲過程中的參數(shù)視為命名參數(shù),并搜索匹配的參數(shù)標記。例如,通過使用名為 @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

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)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

分享文章:如何使用ADO.NET參數(shù)
URL分享:http://sd-ha.com/article34/ggeppe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、企業(yè)網(wǎng)站制作網(wǎng)站設(shè)計公司、定制網(wǎng)站網(wǎng)站收錄、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)