|
C# • Retrieve Products Listing 4. The ProductsDB class's GetProducts() method retrieves GolfClubShack product data from a SQL Server 2000 database. You can call the GetProducts() method directly from an ObjectDataSource server control, which reduces greatly the amount of code you need to write in your ASP.NET Web forms. public SqlDataReader GetProducts(int categoryID) {
SqlConnection myConnection =
new SqlConnection(ConfigurationSettings.
ConnectionStrings["ConnectionString"]);
SqlCommand myCommand =
new SqlCommand("ProductsByCategory",
myConnection);
myCommand.CommandType =
CommandType.StoredProcedure;
SqlParameter parameterCategoryID =
new SqlParameter("@CategoryID",
SqlDbType.Int, 4);
parameterCategoryID.Value = categoryID;
myCommand.Parameters.Add(parameterCategoryID);
myConnection.Open();
SqlDataReader result =
myCommand.ExecuteReader(
CommandBehavior.CloseConnection);
return result;
}
|