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

ADO.NET入門 五大你需要知道的對象

開發(fā) 后端
本文將為您介紹ADO.NET中的五大對象,包括Connection、Command、DataReader、DataAdapter和DataSet這五大對象。這是我們進入ADO.NET開發(fā)的鑰匙。

1.  ADO.NET 3.0 用于訪問和操作數(shù)據(jù)的兩個主要組件是: .NET Framework 數(shù)據(jù)提供程序 (虛線框內(nèi)) 和 DataSet。

.NET Framework 數(shù)據(jù)提供程序是專門為數(shù)據(jù)操作以及快速、只進、只讀訪問數(shù)據(jù)而設計的組件。

ADO.NET DataSet 是專門為獨立于任何數(shù)據(jù)源的數(shù)據(jù)訪問而設計的。

五大對象

對象

說明

Connection

建立與特定數(shù)據(jù)源的連接。 所有 Connection 對象的基類均為 DbConnection 類。

Command

對數(shù)據(jù)源執(zhí)行命令。 公開 Parameters,并可在 Transaction 范圍內(nèi)從 Connection 執(zhí)行。 所有 Command 對象的基類均為 DbCommand 類。

DataReader

從數(shù)據(jù)源中讀取只進且只讀的數(shù)據(jù)流。 所有 DataReader 對象的基類均為 DbDataReader 類。

DataAdapter

使用數(shù)據(jù)源填充 DataSet 并解決更新。 所有 DataAdapter 對象的基類均為 DbDataAdapter 類。

注意:新手面試經(jīng)常會遇到考這樣的題:ADO.NET 的五大對象,就是 上面四種 + DataSet 要牢牢記住哦。后期開發(fā)也經(jīng)常用到。

2. Connection 對象(只介紹SqlConnection和JDBC)

使用connection連接的時候記得打開、關閉(返回連接池),建議使用using,這樣就不會忘記關了,將自動斷開連接,即使發(fā)生無法處理的異常。

string connectionString = "數(shù)據(jù)庫連接字符串";
 
  1. using (SqlConnection connection = new SqlConnection(connectionString))  
  2. {  
  3.     connection.Open();  
  4.     ....  

ODBC 比較麻煩 請參考微軟技術文檔:http://support.microsoft.com/kb/310988 (我很少用到)

  1. using (OdbcConnection connection =   
  2.   new OdbcConnection(connectionString))  
  3. {  
  4.     connection.Open();  
  5.     ....  

3.Command對象

命令

返回值

ExecuteReader

返回一個 DataReader 對象。

ExecuteScalar

返回數(shù)據(jù)庫查詢出來的第一行第一列。

ExecuteNonQuery

執(zhí)行增刪改命令。

ExecuteXMLReader

返回 XmlReader。 只用于 SqlCommand 對象。

下面用一個實例講解Connection 和 Command、DataReader以及儲存過程和參數(shù)的設置:

(1.)下載安裝微軟提供的Northwind數(shù)據(jù)庫:

/Files/Simcoder/微軟提供的數(shù)據(jù)庫.rar 含幫助文檔 簡單容易操作 數(shù)據(jù)庫安裝后 文件默認在C盤 然后附加即可

(2.)找到提供的存儲過程:(本實例 使用倒數(shù)第二個 SalesByCategory 存儲過程做演示)

存儲過程

(3.)簡單查看一下存儲過程的代碼,其實通過名字都能知道大概做什么用

  1. ----------------------  *創(chuàng)*建*存*儲*過*程* -----------------------  
  2. set ANSI_NULLS ON 
  3. set QUOTED_IDENTIFIER ON 
  4. go  
  5. ALTER PROCEDURE [dbo].[SalesByCategory]                           --修改存儲過程[SalesByCategory]  
  6.     @CategoryName nvarchar(15), @OrdYear nvarchar(4) = '1998'     --設置參數(shù)  (以下的實現(xiàn)暫不用管)  
  7. AS 
  8. IF @OrdYear != '1996' AND @OrdYear != '1997' AND @OrdYear != '1998'   
  9. BEGIN 
  10.     SELECT @OrdYear = '1998' 
  11. END 
  12. SELECT ProductName,  
  13.     TotalPurchase=ROUND(SUM(CONVERT(decimal(14,2), OD.Quantity * (1-OD.Discount) * OD.UnitPrice)), 0)  
  14. FROM [Order Details] OD, Orders O, Products P, Categories C  
  15. WHERE OD.OrderID = O.OrderID   
  16.     AND OD.ProductID = P.ProductID   
  17.     AND P.CategoryID = C.CategoryID  
  18.     AND C.CategoryName = @CategoryName  
  19.     AND SUBSTRING(CONVERT(nvarchar(22), O.OrderDate, 111), 1, 4) = @OrdYear  
  20. GROUP BY ProductName  
  21. ORDER BY ProductName  
  22. ----------------------  *執(zhí)*行*存*儲*過*程* -----------------------  
  23. USE [Northwind]  
  24. GO  
  25. DECLARE    @return_value int 
  26. EXEC    @return_value = [dbo].[SalesByCategory]  
  27.         @CategoryName = N'Produce',  
  28.         @OrdYear = N'1998' 
  29. SELECT    'Return Value' = @return_value  
  30. GO 


--需要設置@CategoryName,@OrdYear(可以不設置 為空上面有判斷)參數(shù)值

查詢結(jié)果是:

查詢結(jié)果 

(4.)新建一個控制臺的應用程序 代碼如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Data;  
  7.  
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             GetSalesByCategory("server=.;uid=sa;pwd=123456;database=Northwind""Produce");//在這里就默認設置了 參數(shù)@CategoryName參數(shù)的值為 Produce  
  15.         }  
  16.         static void GetSalesByCategory(string connectionString,string categoryName)  
  17.         {  
  18.             using (SqlConnection connection = new SqlConnection(connectionString))  
  19.             {  
  20.                 SqlCommand command = new SqlCommand();  
  21.                 command.Connection = connection;  
  22.                 command.CommandText = "SalesByCategory";  //CommandType 屬性設置為 StoredProcedure 時,CommandText 屬性應設置為存儲過程的名稱  
  23.  
  24.                 command.CommandType = CommandType.StoredProcedure; //設置執(zhí)行類型為存儲過程  
  25.  
  26.                 SqlParameter parameter = new SqlParameter();  
  27.                 parameter.ParameterName = "@CategoryName";//指定存儲過程中的那個參數(shù)  
  28.                 parameter.SqlDbType = SqlDbType.NVarChar;//指定數(shù)據(jù)類型  
  29.                 parameter.Direction = ParameterDirection.Input;//指定參數(shù)為輸入  
  30.                 parameter.Value = categoryName;  
  31.  
  32.                 command.Parameters.Add(parameter);  
  33.  
  34.                 connection.Open();  
  35.                 SqlDataReader reader = command.ExecuteReader();  
  36.  
  37.                 if (reader.HasRows)//判斷是否有數(shù)據(jù)行  
  38.                 {  
  39.                     while (reader.Read())  
  40.                     {  
  41.                         Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);  
  42.                     }  
  43.                 }  
  44.                 else 
  45.                 {  
  46.                     Console.WriteLine("No rows found.");  
  47.                 }  
  48.                 reader.Close();//記得關閉   
  49.                 Console.ReadLine();  
  50.             }  
  51.         }  
  52.     }  

前面簡單提到了 Connection 、DataReader、Comand以及參數(shù)和存儲過程的用法,現(xiàn)在更加深入的學習。

1.DataReader的用法:

DataReader 從數(shù)據(jù)庫中檢索只讀、只進的數(shù)據(jù)流。查詢結(jié)果在查詢執(zhí)行時返回,在并存儲在客戶端的網(wǎng)絡緩沖區(qū)中,直到您使用 DataReader 的 Read 方法對它們發(fā)出請求。 使用 DataReader 可以提高應用程序的性能,原因是它只要數(shù)據(jù)可用就立即檢索數(shù)據(jù),并且(默認情況下)一次只在內(nèi)存中存儲一行,減少了系統(tǒng)開銷。

例子見上一篇即可,說說使用DataReader的心得,在做項目中,有時候一個實體類中的字段又是另外一個實體雷,存在外鍵的關系。如下實體類源碼 中就有2個這樣的關系(高亮代碼):

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace BookShop.Model  
  6. {  
  7.     [Serializable]  
  8.     public class Book  
  9.     {  
  10.         /// <summary>  
  11.         /// 圖書編號  
  12.         /// </summary>  
  13.         private int id;  
  14.  
  15.         public int Id  
  16.         {  
  17.             get { return id; }  
  18.             set { id = value; }  
  19.         }  
  20.  
  21.         /// <summary>  
  22.         /// 圖書標題  
  23.         /// </summary>  
  24.         private string title;  
  25.  
  26.         public string Title  
  27.         {  
  28.             get { return title; }  
  29.             set { title = value; }  
  30.         }  
  31.  
  32.         /// <summary>  
  33.         /// 圖書作者  
  34.         /// </summary>  
  35.         private string author;  
  36.  
  37.         public string Author  
  38.         {  
  39.             get { return author; }  
  40.             set { author = value; }  
  41.         }  
  42.  
  43.         /// <summary>  
  44.         /// 圖書出版社  
  45.         /// </summary>  
  46.         private Publisher publisher;  
  47.  
  48.         public Publisher Publisher  
  49.         {  
  50.             get { return publisher; }  
  51.             set { publisher = value; }  
  52.         }  
  53.  
  54.         /// <summary>  
  55.         /// 圖書出版日期  
  56.         /// </summary>  
  57.         private DateTime publishDate;  
  58.  
  59.         public DateTime PublishDate  
  60.         {  
  61.             get { return publishDate; }  
  62.             set { publishDate = value; }  
  63.         }  
  64.  
  65.         /// <summary>  
  66.         /// 圖書ISBN編號  
  67.         /// </summary>  
  68.         private string isbn;  
  69.  
  70.         public string Isbn  
  71.         {  
  72.             get { return isbn; }  
  73.             set { isbn = value; }  
  74.         }  
  75.  
  76.         /// <summary>  
  77.         /// 圖書總字數(shù)  
  78.         /// </summary>  
  79.         private int wordsCount;  
  80.  
  81.         public int WordsCount  
  82.         {  
  83.             get { return wordsCount; }  
  84.             set { wordsCount = value; }  
  85.         }  
  86.  
  87.         /// <summary>  
  88.         /// 圖書價格  
  89.         /// </summary>  
  90.         private decimal unitPrice;  
  91.  
  92.         public decimal UnitPrice  
  93.         {  
  94.             get { return unitPrice; }  
  95.             set { unitPrice = value; }  
  96.         }  
  97.  
  98.         /// <summary>  
  99.         /// 圖書描述  
  100.         /// </summary>  
  101.         private string contentDescription;  
  102.  
  103.         public string ContentDescription  
  104.         {  
  105.             get { return contentDescription; }  
  106.             set { contentDescription = value; }  
  107.         }  
  108.  
  109.         /// <summary>  
  110.         /// 圖書作者描述  
  111.         /// </summary>  
  112.         private string authorDescription;  
  113.  
  114.         public string AuthorDescription  
  115.         {  
  116.             get { return authorDescription; }  
  117.             set { authorDescription = value; }  
  118.         }  
  119.  
  120.         /// <summary>  
  121.         /// 圖書作者評語  
  122.         /// </summary>  
  123.         private string editorComment;  
  124.  
  125.         public string EditorComment  
  126.         {  
  127.             get { return editorComment; }  
  128.             set { editorComment = value; }  
  129.         }  
  130.  
  131.         /// <summary>  
  132.         /// 圖書目錄  
  133.         /// </summary>  
  134.         private string toc;  
  135.  
  136.         public string Toc  
  137.         {  
  138.             get { return toc; }  
  139.             set { toc = value; }  
  140.         }  
  141.  
  142.         /// <summary>  
  143.         /// 圖書的分類  
  144.         /// </summary>  
  145.         private Category category;  
  146.  
  147.         public Category Category  
  148.         {  
  149.             get { return category; }  
  150.             set { category = value; }  
  151.         }  
  152.  
  153.         /// <summary>  
  154.         /// 圖書點擊  
  155.         /// </summary>  
  156.         private int clicks;  
  157.  
  158.         public int Clicks  
  159.         {  
  160.             get { return clicks; }  
  161.             set { clicks = value; }  
  162.         }  
  163.  
  164.     }  

如果是這種關系,使用Datareader 就可能會出現(xiàn)異常,因為當代碼讀到 外鍵的時候,外鍵也要使用connection連接 這時就會拋出異常,所以

與數(shù)據(jù)進行動態(tài)交互,例如綁定到 Windows 窗體控件或組合并關聯(lián)來自多個源的數(shù)據(jù)。

對數(shù)據(jù)執(zhí)行大量的處理,而不需要與數(shù)據(jù)源保持打開的連接,從而將該連接釋放給其他客戶端使用。就使用DataSet或DataTable比較合適。

也許你不太明白,但是你可以這樣簡單的記住,當實體類或數(shù)據(jù)庫設計存在主外鍵關系的時候,使用Datareader就要謹慎了! 不過也沒關系,很多經(jīng)驗都是從Debug學到的。

就好像微軟的視頻一樣,為愛Debug。

原文標題:ADO.NET快速上手(一)

鏈接:http://www.cnblogs.com/Simcoder/archive/2010/05/03/1726295.html

責任編輯:彭凡 來源: 博客園
相關推薦

2009-11-13 15:12:54

ADO.NET入門

2022-04-22 08:00:00

TEE安卓手機

2009-11-04 09:43:45

ADO.NET Dat

2009-11-12 15:55:31

ADO.NET對象服務

2009-11-11 13:46:41

ADO.NET異步查詢

2018-12-27 14:21:31

https安全http

2019-11-15 14:00:39

HTTPSHTTP前端

2018-12-28 14:16:11

安全

2009-10-29 13:34:01

ADO.NET對象

2009-06-30 13:00:30

JSP入門

2009-11-04 12:45:33

ADO.NET Dat

2009-04-28 10:08:28

ADO.NET對象微軟

2009-11-11 14:27:32

ADO.NET函數(shù)

2009-11-11 10:27:22

ADO.NET入門

2009-11-13 15:25:51

ADO.NET的對象

2009-11-12 16:04:42

ADO.NET對象查詢

2009-11-04 11:30:35

ADO.NET Dat

2009-12-28 13:47:31

ADO.NET對象

2009-12-18 14:27:24

ADO.NET對象

2009-12-21 17:35:24

ADO.NET對象
點贊
收藏

51CTO技術棧公眾號