SqlDataReader.Read 方法


SqlDataReader 推進到下一筆記錄。Advances the SqlDataReader to the next record.

 virtual bool Read();
public bool Read ();
abstract member Read : unit -> bool
override this.Read : unit -> bool
Public Function Read () As Boolean


如果有多個資料列則為 true;否則為 falsetrue if there are more rows; otherwise false.



SQL Server 於執行命令文字時傳回錯誤。SQL Server returned an error while executing the command text.


下列範例會建立一個 SqlConnection、一個 SqlCommand和一個 SqlDataReaderThe following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. 此範例會讀取資料,並將其寫入至主控台視窗。The example reads through the data, writing it out to the console window. 然後,程式碼會關閉 SqlDataReaderThe code then closes the SqlDataReader. SqlConnection 會在 using 程式碼區塊的結尾自動關閉。The SqlConnection is closed automatically at the end of the using code block.

using System;
using System.Data;
using System.Data.SqlClient;

class Program
    static void Main()
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";

    private static void ReadOrderData(string connectionString)
        string queryString =
            "SELECT OrderID, CustomerID FROM dbo.Orders;";

        using (SqlConnection connection =
                   new SqlConnection(connectionString))
            SqlCommand command =
                new SqlCommand(queryString, connection);

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            while (reader.Read())

            // Call Close when done reading.

    private static void ReadSingleRow(IDataRecord record)
        Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));

Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Module Module1

    Sub Main()
        Dim str As String = "Data Source=(local);Initial Catalog=Northwind;" _
       & "Integrated Security=SSPI;"
    End Sub

    Private Sub ReadOrderData(ByVal connectionString As String)
        Dim queryString As String = _
            "SELECT OrderID, CustomerID FROM dbo.Orders;"

        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, connection)

            Dim reader As SqlDataReader = command.ExecuteReader()

            ' Call Read before accessing data.
            While reader.Read()
                ReadSingleRow(CType(reader, IDataRecord))
            End While

            ' Call Close when done reading.
        End Using
    End Sub

    Private Sub ReadSingleRow(ByVal record As IDataRecord)
       Console.WriteLine(String.Format("{0}, {1}", record(0), record(1)))

    End Sub

End Module


SqlDataReader 的預設位置是在第一筆記錄之前。The default position of the SqlDataReader is before the first record. 因此,您必須呼叫 Read,才能開始存取任何資料。Therefore, you must call Read to begin accessing any data.

一次只能開啟一個相關聯的 SqlConnection SqlDataReader,而任何開啟另一個的嘗試都將失敗,直到第一個的關閉為止。Only one SqlDataReader per associated SqlConnection may be open at a time, and any attempt to open another will fail until the first one is closed. 同樣地,當使用 SqlDataReader 時,相關聯的 SqlConnection 會忙於服務,直到您呼叫 Close為止。Similarly, while the SqlDataReader is being used, the associated SqlConnection is busy serving it until you call Close.