天天看點

C#之讀取資料:DataReader對象

     上一篇涉及到Command對象的ExecuteReader()方法傳回一個DataReader對象,那麼我們就來詳細的介紹這個DataReade對象。

        下面的例子使用的資料表依然與上篇的相同為CustomerManagement資料庫中的manager資料表:

C#之讀取資料:DataReader對象

        DataReader對象概述

        DataReader對象提供了順序的,隻讀的方式讀取Command對象獲得的資料結果集。正是因為DataReader是以順序的方式連續地讀取資料,是以DataReader會以獨占的方式打開資料庫連接配接。

        由于DataReader隻執行讀操作,并且每次隻在記憶體緩沖區裡存儲結果集的一條資料,是以使用Datareader對象的效率比較高,如果要查詢大量資料,同僚不需要随機通路和修改資料,DataReader是優先的選擇。DataReader對象有許多的屬性和方法:

C#之讀取資料:DataReader對象

        判斷查詢結果中是否有資料

        想要判斷查詢結果中是否有資料隻需要判斷DataReader對象的HasRows屬性即可。

        例一,使用上述的方法判斷CustomerManagement資料庫中的manager資料表是否有資料的完整代碼為:

[csharp]  view plain  copy

  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.SqlClient;//引入命名空間  
  7. namespace ConsoleApplication1  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串  
  14.             SqlConnection conText = new SqlConnection(conStr);//建立Connection對象  
  15.             try  
  16.             {  
  17.                 conText.Open();//打開資料庫  
  18.                 string sql = "select * from manager";//建立統計語句  
  19.                 SqlCommand comText = new SqlCommand(sql, conText);//建立Command對象  
  20.                 SqlDataReader dr;//建立DataReader對象  
  21.                 dr=comText.ExecuteReader();//執行查詢  
  22.                 if (dr.HasRows)//判斷資料表中是否含有資料  
  23.                 {  
  24.                     Console.WriteLine("manager資料表中含有資料");  
  25.                 }  
  26.                 else  
  27.                 {  
  28.                     Console.WriteLine("manager資料表中沒有資料");  
  29.                 }  
  30.             }  
  31.             catch (Exception ex)//建立檢查Exception對象  
  32.             {  
  33.                 Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊  
  34.             }  
  35.             finally  
  36.             {  
  37.                 conText.Close();//關閉連接配接  
  38.             }  
  39.             Console.ReadLine();  
  40.         }  
  41.     }  
  42. }</span>  

        運作結果為:

C#之讀取資料:DataReader對象

        讀取資料

        要想讀取DataReader對象中的資料,就要用到DataReader對象的Read方法,由于DataReader對象每次隻在記憶體緩沖區裡存儲結果集中的一條資料,是以要讀取DataReader對象中的多條資料,就要用到疊代語句。

        例二,通過DataReader對象的Read方法讀取CustomerManagement資料庫中的manager資料表中的資料的完整代碼為:

[csharp]  view plain  copy

  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.SqlClient;//引入命名空間  
  7. namespace ConsoleApplication1  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串  
  14.             SqlConnection conText = new SqlConnection(conStr);//建立Connection對象  
  15.             try  
  16.             {  
  17.                 conText.Open();//打開資料庫  
  18.                 string sql = "select * from manager";//建立統計語句  
  19.                 SqlCommand comText = new SqlCommand(sql, conText);//建立Command對象  
  20.                 SqlDataReader dr;//建立DataReader對象  
  21.                 dr=comText.ExecuteReader();//執行查詢  
  22.                 while(dr.Read())//判斷資料表中是否含有資料  
  23.                 {  
  24.                     Console.Write(dr[0].ToString()+",");//輸出使用者辨別  
  25.                     Console.Write(dr["userName"].ToString()+",");//輸出使用者名  
  26.                     Console.WriteLine(dr[2].ToString());//輸出使用者密碼  
  27.                 }  
  28.                 dr.Close();//關閉DataReader對象  
  29.             }  
  30.             catch (Exception ex)//建立檢查Exception對象  
  31.             {  
  32.                 Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊  
  33.             }  
  34.             finally  
  35.             {  
  36.                 conText.Close();//關閉連接配接  
  37.             }  
  38.             Console.ReadLine();  
  39.         }  
  40.     }  
  41. }  
  42. </span>  

        運作的結果為:

C#之讀取資料:DataReader對象

        與CustomerManagement資料庫中的manage資料表中的資料比較得出讀取的結果保持一緻。

C#之讀取資料:DataReader對象