[ADO.NET] 如何使用 SQLDataReader 讀取資料 / 取得多個結果集

[ADO.NET] 如何使用 SQLDataReader 讀取資料 / 取得多個結果集

1.DataReader 讀取的資料屬於唯讀

2.DataReader 必須要搭配 Command 物件使用,故為連線資料存取

3.DataReader 一次只針對一筆資料列作業

4.在SQL Server 7以上版本的請使用 GetSql 開頭的方法,會比Get開頭的效能還要好。

listBox1.Items.Add(dr.GetSqlInt32(0).ToString() + "\t" + dr.GetSqlString(1).ToString());

5.GetOrdinal方法,是以"欄位名稱"找尋對應的此欄位的"索引值"。使用"索引值"來讀取資料會比使用"欄位名稱"還要來得快,那萬一索引值變了呢?所以使用GetOrdinal方法+索引值將會增加效能。

6.以Get開頭的方法讀取資料時,若欄位資料為空,會造成例外,所以我們要判斷DataReader取回的欄位是否為空:

    6-1.使用DbNull.Value

    6-2.使用IsDbNull

7.若要取的多結果集則使用NextResult方法

  

以下範例需引用 System.Data.SqlClient命名空間

如何使用 SQLDataReader 讀取資料

1.引用SqlConnection物件連接資料庫
2.開啟資料庫
3.引用SqlCommand物件
4.搭配SqlCommand物件使用SqlDataReader
5.判斷資料列是否為空

        private void button1_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            string cs = "server=localhost\\sqlexpress;database=Northwind;integrated security=SSPI;";

            //string qs = "SELECT myregion,myname FROM mytable;";

            string qs = "select * from employees";

            //1.引用SqlConnection物件連接資料庫

            using (SqlConnection cn = new SqlConnection(cs))

            {

                //2.開啟資料庫

                cn.Open();

                //3.引用SqlCommand物件

                using (SqlCommand command = new SqlCommand(qs, cn))

                {

                    //4.搭配SqlCommand物件使用SqlDataReader

                    using (SqlDataReader dr = command.ExecuteReader())

                    {

                        while ((dr.Read()))

                        {

                            //5.判斷資料列是否為空

                            if (!dr[0].Equals(DBNull.Value))

                            {

                                //listBox1.Items.Add(dr[0].ToString() + "\t" + dr[1].ToString());

                                //listBox1.Items.Add(dr["EmployeeID"].ToString() + "\t" + dr["EmployeeName"].ToString());

                                //listBox1.Items.Add(dr.GetInt32(0).ToString() + "\t" + dr.GetString(1).ToString());

                                //listBox1.Items.Add(dr.GetSqlInt32(0).ToString() + "\t" + dr.GetSqlString(1).ToString());

                                listBox1.Items.Add(dr[0].ToString() + "\t" + dr[1].ToString() + "\t" + dr[2].ToString() + "\t" + dr[3].ToString());

                            }

                        }

                    }

                }

            }

        }

如何使用 SQLDataReader 取得多個結果集

方法同上,不同的是qs變數是取兩個資料表,再搭配NextResult方法

        private void button3_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            listBox2.Items.Clear();

            string cs = "server=localhost\\sqlexpress;database=Northwind;integrated security=SSPI;";

            string qs = "select * from employees;select * from products";

 

            //1.引用SqlConnection物件連接資料庫

            using (SqlConnection cn = new SqlConnection(cs))

            {

                //2.開啟資料庫

                cn.Open();

                //3.引用SqlCommand物件

                using (SqlCommand command = new SqlCommand(qs, cn))

                {

                    //4.搭配SqlCommand物件使用SqlDataReader

                    using (SqlDataReader dr = command.ExecuteReader())

                    {

                        while ((dr.Read()))

                        {

                            //5.判斷資料列是否為空

                            if (!dr[0].Equals(DBNull.Value))

                            {

                                listBox1.Items.Add(dr[0].ToString() + "\t" + dr[1].ToString() + "\t" + dr[2].ToString() + "\t" + dr[3].ToString());

                            }

                        }

                        //6.下一個查詢結果

                        dr.NextResult();

                        while ((dr.Read()))

                        {

                            if (!dr[0].Equals(DBNull.Value))

                            {

                                listBox2.Items.Add(dr[0].ToString() + "\t" + dr[1].ToString() + "\t" + dr[2].ToString() + "\t" + dr[3].ToString());

                            }

                        }

                    }

                }

            }

        }

 

如何利用GetOrdinal方法取的對應的索引值

        private void button2_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            string cs = "server=localhost\\sqlexpress;database=Northwind;integrated security=SSPI;";

            string qs = "select * from employees";

            //1.引用SqlConnection物件連接資料庫

            using (SqlConnection cn = new SqlConnection(cs))

            {

                //2.開啟資料庫

                cn.Open();

                //3.引用SqlCommand物件

                using (SqlCommand command = new SqlCommand(qs, cn))

                {

                    //4.搭配SqlCommand物件使用SqlDataReader

                    using (SqlDataReader dr = command.ExecuteReader())

                    {

                        while ((dr.Read()))

                        {

                            int intID = dr.GetOrdinal("employeeid");

                            int intDesc = dr.GetOrdinal("lastname");

                            listBox1.Items.Add(dr[intID].ToString() + " – " + dr[intDesc].ToString());

                        }

                    }

                }

            }

        }

 

快照-20096131160

範例下載:SQLDataReader.rar

以上範例僅供參考,請依實際狀況修改

資料庫請於 [ADO.NET] 如何 使用 SQLCommand 查詢資料庫 下載

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo