using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand selectCommand = factory.CreateCommand())
{
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.CommandText = “SalesByCategory”;
selectCommand.Connection = connection;
DbParameter categoryParameter = selectCommand.CreateParameter();
categoryParameter.ParameterName = “@CategoryName”;
categoryParameter.Value = “Beverages”;
DbParameter yearParameter = selectCommand.CreateParameter();
yearParameter.ParameterName = “@OrdYear”;
yearParameter.Value = “1998”;
selectCommand.Parameters.AddRange(new[] {categoryParameter, yearParameter});
connection.Open();
using (IDataReader dr = selectCommand.ExecuteReader())
{
while (dr.Read())
Console.WriteLine(string.Format(“{0}: {1:c}”, dr[“ProductName”], dr[“TotalPurchase”]));
}
}
}
Compare this to using the Data Access Application Block in Enterprise Library:
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader dr = db.ExecuteReader(“SalesByCategory”, “Beverages”, “1998”))
{
while (dr.Read())
Console.WriteLine(string.Format(“{0}: {1:c}”, dr[“ProductName”], dr[“TotalPurchase”]));
}
source code : http://www.pnpguidance.net/post/EnterpriseLibraryDAABADONETStoredProceduresMadeEasy.aspx