上一篇涉及到Command對象的ExecuteReader()方法傳回一個DataReader對象,那麼我們就來詳細的介紹這個DataReade對象。
下面的例子使用的資料表依然與上篇的相同為CustomerManagement資料庫中的manager資料表:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zZNVzaU1UdGdVYzpEWaZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jN3MTMxkDN2EzMyUDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
DataReader對象概述
DataReader對象提供了順序的,隻讀的方式讀取Command對象獲得的資料結果集。正是因為DataReader是以順序的方式連續地讀取資料,是以DataReader會以獨占的方式打開資料庫連接配接。
由于DataReader隻執行讀操作,并且每次隻在記憶體緩沖區裡存儲結果集的一條資料,是以使用Datareader對象的效率比較高,如果要查詢大量資料,同僚不需要随機通路和修改資料,DataReader是優先的選擇。DataReader對象有許多的屬性和方法:
判斷查詢結果中是否有資料
想要判斷查詢結果中是否有資料隻需要判斷DataReader對象的HasRows屬性即可。
例一,使用上述的方法判斷CustomerManagement資料庫中的manager資料表是否有資料的完整代碼為:
[csharp] view plain copy
- <span style="font-size:18px;">using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.SqlClient;//引入命名空間
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串
- SqlConnection conText = new SqlConnection(conStr);//建立Connection對象
- try
- {
- conText.Open();//打開資料庫
- string sql = "select * from manager";//建立統計語句
- SqlCommand comText = new SqlCommand(sql, conText);//建立Command對象
- SqlDataReader dr;//建立DataReader對象
- dr=comText.ExecuteReader();//執行查詢
- if (dr.HasRows)//判斷資料表中是否含有資料
- {
- Console.WriteLine("manager資料表中含有資料");
- }
- else
- {
- Console.WriteLine("manager資料表中沒有資料");
- }
- }
- catch (Exception ex)//建立檢查Exception對象
- {
- Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊
- }
- finally
- {
- conText.Close();//關閉連接配接
- }
- Console.ReadLine();
- }
- }
- }</span>
運作結果為:
讀取資料
要想讀取DataReader對象中的資料,就要用到DataReader對象的Read方法,由于DataReader對象每次隻在記憶體緩沖區裡存儲結果集中的一條資料,是以要讀取DataReader對象中的多條資料,就要用到疊代語句。
例二,通過DataReader對象的Read方法讀取CustomerManagement資料庫中的manager資料表中的資料的完整代碼為:
[csharp] view plain copy
- <span style="font-size:18px;">using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.SqlClient;//引入命名空間
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串
- SqlConnection conText = new SqlConnection(conStr);//建立Connection對象
- try
- {
- conText.Open();//打開資料庫
- string sql = "select * from manager";//建立統計語句
- SqlCommand comText = new SqlCommand(sql, conText);//建立Command對象
- SqlDataReader dr;//建立DataReader對象
- dr=comText.ExecuteReader();//執行查詢
- while(dr.Read())//判斷資料表中是否含有資料
- {
- Console.Write(dr[0].ToString()+",");//輸出使用者辨別
- Console.Write(dr["userName"].ToString()+",");//輸出使用者名
- Console.WriteLine(dr[2].ToString());//輸出使用者密碼
- }
- dr.Close();//關閉DataReader對象
- }
- catch (Exception ex)//建立檢查Exception對象
- {
- Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊
- }
- finally
- {
- conText.Close();//關閉連接配接
- }
- Console.ReadLine();
- }
- }
- }
- </span>
運作的結果為:
與CustomerManagement資料庫中的manage資料表中的資料比較得出讀取的結果保持一緻。