dotConnect for SQL Server Documentation
Devart.Data.SqlServer Namespace / SqlDataReader Class
Members Example

In This Topic
    SqlDataReader Class
    In This Topic
    Reads a forward-only stream of rows from SQL Server.
    Syntax
    Remarks
    To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, rather than directly using a constructor.

    System.Data.Common.DbDataReader.IsClosed and RecordsAffected are the only properties that you can call after the SqlDataReader is closed. In some cases, you must call Close before you can call RecordsAffected.

    While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until you call Close.

    Example
    The following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the SqlDataReader, then the SqlConnection.
    public void ReadMyData(string myConnString)  {
            SqlConnection myConnection = new SqlConnection(myConnString);
            SqlCommand myCommand = (SqlCommand)myConnection.CreateCommand();
            myCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept";
            myConnection.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
            try {
                    // Always call Read before accessing data.
                    while (myReader.Read()) {
                    Console.WriteLine(myReader.GetInt32(0).ToString() + " " + 
                    myReader.GetString(1) + " " + myReader.GetString(2));
            }
            }
            finally {
                    // always call Close when done reading.
                    myReader.Close();
    
                    // Close the connection when done with it.
                    myConnection.Close();
            }
    }
    Public Sub ReadMyData(ByVal myConnString As String)
            Dim myConnection As New SqlConnection(myConnString)
            Dim myCommand As SqlCommand = myConnection.CreateCommand()
                    myCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept"
            myConnection.Open()
            Dim myReader As SqlDataReader = myCommand.ExecuteReader()
            Try
                    ' Always call Read before accessing data.
                    While myReader.Read()
                            Console.WriteLine(String.Concat(myReader.GetInt32(0).ToString(), " ", _
                            myReader.GetString(1), " ", myReader.GetString(2)))
                    End While
            Finally
                    ' always call Close when done reading.
                    myReader.Close()
    
                            ' Close the connection when done with it.
                            myConnection.Close()
            End Try
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbDataReader
             Devart.Common.DbDataReaderBase
                Devart.Data.SqlServer.SqlDataReader

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also