SqlDataReader.Read 方法

定義

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

public:
 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";
        ReadOrderData(str);
    }

    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);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            while (reader.Read())
            {
                ReadSingleRow((IDataRecord)reader);
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

    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;"
        ReadOrderData(str)
    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)
            connection.Open()

            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.
            reader.Close()
        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.

適用於

另請參閱