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이고, 그렇지 않으면 false입니다.true if there are more rows; otherwise false.



SQL Server에서 명령 텍스트를 실행하는 동안 오류를 반환했습니다.SQL Server returned an error while executing the command text.


다음 예에서는 SqlConnection, SqlCommandSqlDataReader를 만듭니다.The following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. 이 예제에서는 데이터를 읽고 콘솔 창에 기록 합니다.The example reads through the data, writing it out to the console window. 그런 다음 코드는 SqlDataReader를 닫습니다.The 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를 사용 하는 동안에는 Close를 호출할 때까지 연결 된 SqlConnection 사용 됩니다.Similarly, while the SqlDataReader is being used, the associated SqlConnection is busy serving it until you call Close.

적용 대상

추가 정보