October 10, 2002 - Retrieving Selected Products from the IBuySpy Database
![]() |
October 10, 2002 Retrieving Selected Products from the IBuySpy Database Tips: October 2002
Yehuda Shiran, Ph.D.
|
SqlConnection object.
SqlCommand object.
Value parameter.
SqlDataReader result to the caller.
Let's take an example. The ProductsDB.js file includes the method GetProducts(categoryID: int). As its name implies, this method retrieves all products belonging to categoryID. It follows the recipe above, calling the stored procedure ProductsByCategory. This stored procedure expects a single parameter, CategoryID. It returns a table of all products with the given CategoryID. Here is the code:
public function GetProducts(categoryID: int) : SqlDataReader {
var myConnection : SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
var myCommand : SqlCommand = new SqlCommand("ProductsByCategory", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
var parameterCategoryID : SqlParameter = new SqlParameter("@CategoryID", SqlDbType.Int, 4);
parameterCategoryID.Value = categoryID;
myCommand.Parameters.Add(parameterCategoryID);
myConnection.Open();
var result : SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}


Find a programming school near you