Joel.Net.DatabaseUtility.dll Class Library

DatabaseUtility.ExecuteReader Method (SqlConnection, String)

Sends the System.Data.SqlClient.SqlCommand.CommandText to the System.Data.SqlClient.SqlCommand.Connection, and builds a System.Data.SqlClient.SqlDataReader.

[Visual Basic]
Shared  OverloadsPublic Function ExecuteReader( _ 
   ByVal connection As SqlConnection, _ 
   ByVal commandText As String _ 
) As SqlDataReader
[C#]
public static SqlDataReader ExecuteReader(
   SqlConnection connection,
   string commandText
);
[C++]
public: static SqlDataReader* ExecuteReader(
   SqlConnection* connection,
   String* commandText
);
[JScript]
public static function ExecuteReader(
   SqlConnection connection,
   String commandText
): SqlDataReader;

Parameters

connection
Represents an open connection to a SQL Server database.
commandText
The text of the query.

Return Value

A System.Data.SqlClient.SqlDataReader object.

Example

[C#, Visual Basic] The following example creates a SqlCommand, then executes it by passing a string that is a Transact-SQL SELECT statement, and a string to use to connect to the data source. CommandBehavior is set to CloseConnection.

[C#]
SqlConnection connection = new SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;");
SqlDataReader reader = DatabaseUtility.ExecuteReader(connection, "SELECT * FROM Customers");

while (reader.Read()) {
    Console.WriteLine("ExecuteReader: {0}, {1}, {2}", reader["CustomerID"], reader["CompanyName"], reader["ContactName"]);
}

reader.Close(); // this will close the connection (only if connection was not opened before ExecuteReader)
[Visual Basic]
Dim connection As New SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;")
SqlDataReader reader = DatabaseUtility.ExecuteReader(connection, "SELECT * FROM Customers")

While (reader.Read()) 
    Console.WriteLine("ExecuteReader: {0}, {1}, {2}", reader["CustomerID"], reader["CompanyName"], reader["ContactName"])
End While

reader.Close() // this will close the connection (only if connection was not opened before ExecuteReader)

See Also

DatabaseUtility Class | Joel.Net Namespace | DatabaseUtility.ExecuteReader Overload List