// Takes a DbConnection and creates a DbCommand to retrieve data
// from the Categories table by executing a DbDataReader.
static void DbCommandSelect(DbConnection connection)
{
string queryString =
"SELECT CategoryID, CategoryName FROM Categories";
// Check for valid DbConnection.
if (connection != null)
{
using (connection)
{
try
{
// Create the command.
DbCommand command = connection.CreateCommand();
command.CommandText = queryString;
command.CommandType = CommandType.Text;
// Open the connection.
connection.Open();
// Retrieve the data.
DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine( "{0}. {1}", reader[0], reader[1]);
}
}
catch (Exception ex)
{
Console.WriteLine( "Exception.Message: {0}", ex.Message);
}
}
}
else
{
Console.WriteLine( "Failed: DbConnection is null.");
}
}
// from the Categories table by executing a DbDataReader.
static void DbCommandSelect(DbConnection connection)
{
string queryString =
"SELECT CategoryID, CategoryName FROM Categories";
// Check for valid DbConnection.
if (connection != null)
{
using (connection)
{
try
{
// Create the command.
DbCommand command = connection.CreateCommand();
command.CommandText = queryString;
command.CommandType = CommandType.Text;
// Open the connection.
connection.Open();
// Retrieve the data.
DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine( "{0}. {1}", reader[0], reader[1]);
}
}
catch (Exception ex)
{
Console.WriteLine( "Exception.Message: {0}", ex.Message);
}
}
}
else
{
Console.WriteLine( "Failed: DbConnection is null.");
}
}