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

演示ADO.NET使用存儲過程獲取數(shù)據(jù)

開發(fā) 后端
這里就ADO.NET使用簡單存儲過程獲取數(shù)據(jù)做出了詳細的分析,大家仔細閱讀,詳細一定會給大家?guī)砑夹g上的提高的。

ADO.NET經(jīng)過長時間的發(fā)展,很多用戶都很了解ADO.NET了,這里我發(fā)表一下個人理解,和大家討論討論。代碼并不創(chuàng)建 Connection 對象或 Command 對象。事實上,沒有這些對象,ADO.NET 便無法工作,但它們是在后臺創(chuàng)建并使用的。實例化 SqlDataAdapter 的代碼行傳入 SQL 字符串(用于配置后臺 Command 對象)和連接字符串(用于配置后臺 Connection 對象)。

我們可以將此代碼更改為使用顯式 Connection 和 Command 對象,以便稍稍遠離演示軟件。在表單上再放置一個按鈕,并將以下代碼放到 Click 事件中。

  1. Dim sConnectionString As String = _ 
  2. "server=localhost;uid=sa;pwd=;database=Northwind"  
  3. Dim sSQL As String = "SELECT * FROM Products" 
  4.  
  5. Dim cnNorthwind As New SqlConnection(sConnectionString)  
  6. Dim cmdProducts As New SqlCommand(sSQL, cnNorthwind)  
  7.  
  8. Dim daGetProducts As New SqlDataAdapter(cmdProducts)  
  9. Dim dsProducts As New DataSet()  
  10. daGetProducts.Fill(dsProducts, "Products")  
  11. DataGrid1.DataSource = dsProducts.Tables("Products")  

#T#此代碼通過顯式創(chuàng)建 Connection 和 Command 對象,并將這些對象附加到 DataAdapter,說明了 DataAdapters 的常用性。通過在實例化 DataAdapter 時傳入 cmdProducts,DataAdapter 的 SelectCommand 將自動設置。然后,可以立即使用 DataAdapter 訪問數(shù)據(jù)庫。此代碼的結(jié)果與前一示例中的結(jié)果相同。盡管它有點接近真實軟件,但由于數(shù)據(jù)訪問是通過 SQL 語句實現(xiàn)的,因此仍然屬于演示軟件。

ADO.NET使用簡單存儲過程獲取數(shù)據(jù)

如何將此演示軟件更改為ADO.NET使用存儲過程?只需更改幾行代碼。在表單上再放置一個按鈕,并將以下代碼放到 Click 事件中:

  1. Dim sConnectionString As String = _ 
  2. "server=localhost;uid=sa;pwd=;database=Northwind"  
  3. Dim cnNorthwind As New SqlConnection(sConnectionString)  
  4. Dim cmdProducts As New _  
  5. SqlCommand("十件最貴的產(chǎn)品", cnNorthwind)  
  6. cmdProducts.CommandType = CommandType.StoredProcedure  
  7.  
  8. Dim daGetProducts As New SqlDataAdapter(cmdProducts)  
  9. Dim dsProducts As New DataSet()  
  10. daGetProducts.Fill(dsProducts, "Products")  
  11. DataGrid1.DataSource = dsProducts.Tables("Products")  

實例化 Command 對象時,此代碼不使用 SQL 語句并替換為要ADO.NET使用的存儲過程名稱。此外,Command 對象的 CommandType 屬性必須設置為 StoredProcedure。此后的代碼與上一個示例非常相似,但它返回不同的數(shù)據(jù)。存儲過程查找十件最貴的產(chǎn)品,并只返回每個產(chǎn)品的名稱和價格。

責任編輯:田樹 來源: 博客
相關推薦

2009-11-04 09:02:34

ADO.NET _C

2009-11-12 09:51:59

ADO.NET結(jié)構

2009-11-11 11:08:03

ADO.NET存儲過程

2009-11-04 16:23:09

ADO.NET存儲過程

2009-11-04 11:30:35

ADO.NET Dat

2009-11-13 11:18:22

ADO.NET修改數(shù)據(jù)

2009-10-29 10:00:53

ADO.NET數(shù)據(jù)集

2009-11-03 15:13:13

ADO .NET存儲過

2009-11-13 10:01:50

ADO.NET CAS

2009-11-12 10:06:01

ADO.NET讀取數(shù)據(jù)

2009-12-28 15:11:36

ADO.NET專家

2009-12-22 16:50:44

ADO.NET元素

2009-11-12 10:24:19

ADO.NET代碼

2009-11-11 11:27:02

ADO.NET存儲過程

2009-11-12 13:26:56

使用ADO.NET參數(shù)

2009-11-11 13:38:04

ADO.NET sql

2010-01-04 13:47:18

ADO.NET數(shù)據(jù)集

2009-10-29 10:20:19

ADO.NET使用

2009-12-31 09:18:23

ADO.NET對象模型

2009-11-13 10:31:07

ADO.NET Dat
點贊
收藏

51CTO技術棧公眾號