dotConnect for SQL Server Documentation
Devart.Data.SqlServer Namespace / SqlCommand Class / ExecuteReader Method / ExecuteReader() Method
Example

In This Topic
    ExecuteReader() Method
    In This Topic
    Sends the CommandText to the Connection and builds a SqlDataReader.
    Syntax
    'Declaration
     
    Public Overloads Shadows Function ExecuteReader() As SqlDataReader
    public new SqlDataReader ExecuteReader()

    Return Value

    A SqlDataReader object.
    Remarks

    When Devart.Data.SqlServer.SqlCommand.FetchAll property is false the following restriction takes place. While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader. While in this state, no operations can be performed on the SqlConnection except closing. This is the case until the Close method of the SqlDataReader is called.

    Example
    The following example creates a SqlCommand, then executes it by passing a string that is SQL SELECT statement, and a string to use to connect to the data source.
    public void CreateSqlDataReader(string mySelectQuery,string myConnectionString)
    {
      SqlConnection myConnection = new SqlConnection(myConnectionString);
      SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
      myCommand.Connection.Open();
      SqlDataReader myReader = myCommand.ExecuteReader();
      try
      {
        while(myReader.Read())
        {
          Console.WriteLine(myReader.GetString(0));
        }
      }
      finally
      {
        myReader.Close();
        myConnection.Close();
      }
    }
    Public Sub CreateSqlDataReader(mySelectQuery As String, _
    myConnectionString As String)
      Dim myConnection As New SqlConnection(myConnectionString)
      Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
      myCommand.Connection.Open()
      Dim myReader As SqlDataReader = myCommand.ExecuteReader()
      Try
        While myReader.Read()
          Console.WriteLine(myReader.GetString(0))
        End While
      Finally
        myReader.Close()
        myConnection.Close()
      End Try
    End Sub
    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