spacer
Yehuda Shiran October 11, 2002
Retrieving Product Details from the IBuySpy Database
Tips: October 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

A common interaction with an MSSQL database is to retrieve records from the MSSQL database and return them in an sqlDataReader instance object. The JScript .NET application will then use this object to iterate over the records and display them in a tabular format. Sometimes, though, you may want to retrieve single attributes from the database, as opposed to whole records. Since you do all your interactions with the database through stored procedures, you need to write a stored procedure with output parameters. In your JScript .NET code, you need to create an sqlParameter instance object for each output parameter. We have shown yesterday how to define an input parameter. The difference between defining an input parameter and an output parameter is that, while the direction of an input parameter is the default set during the creation of the SqlParameter object, you need to assign the Direction property of the SqlParameter object to the constant ParameterDirection.Output for output parameters.

Here is the full recipe to retrieve single attributes from the MSSQL database:

  1. Create an instance of a connection to the database, an SqlConnection object.
  2. Create an instance of an SQL command, an SqlCommand object.
  3. Mark the command as a stored procedure.
  4. For each input parameter:
    1. Create a parameter instance object.
    2. Set the parameter instance object's Value parameter.
    3. Add the parameter instance object to the SQL command object, created above.
  5. For each output parameter:
    1. Create a parameter instance object.
    2. Set the parameter instance object's Direction parameter to the constant ParameterDirection.Output
    3. Add the parameter instance object to the SQL command object, created above.
  6. Execute the command.

Let's look at an example. The ProductsDB.js file includes the method GetProductDetail(productID: int). As its name implies, this method retrieves the details of a product with ID productID. It follows the recipe above, calling the stored procedure ProductDetail. This stored procedure expects a single input parameter, ProductID. It returns five output parameters: UnitCost, ModelNumber, ModelName, ProductImage, and Description. Here is the code:

  public function GetProductDetails(productID: int) : ProductDetails {
    var myConnection : SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    var myCommand : SqlCommand = new SqlCommand("ProductDetail", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    var parameterProductID : SqlParameter = new SqlParameter("@ProductID", SqlDbType.Int, 4);
    parameterProductID.Value = productID;
    myCommand.Parameters.Add(parameterProductID);
    var parameterUnitCost : SqlParameter = new SqlParameter("@UnitCost", SqlDbType.Money, 8);
    parameterUnitCost.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterUnitCost);
    var parameterModelNumber : SqlParameter = new SqlParameter("@ModelNumber", SqlDbType.NVarChar, 50);
    parameterModelNumber.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterModelNumber);
    var parameterModelName : SqlParameter = new SqlParameter("@ModelName", SqlDbType.NVarChar, 50);
    parameterModelName.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterModelName);
    var parameterProductImage : SqlParameter  = new SqlParameter("@ProductImage", SqlDbType.NVarChar, 50);
    parameterProductImage.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterProductImage);
    var parameterDescription : SqlParameter  = new SqlParameter("@Description", SqlDbType.NVarChar, 3800);
    parameterDescription.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterDescription);
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
    var myProductDetails : ProductDetails  = new ProductDetails();
    myProductDetails.ModelNumber = parameterModelNumber.Value;
    myProductDetails.ModelName = parameterModelName.Value;
    myProductDetails.ProductImage = (parameterProductImage.Value).Trim();
    myProductDetails.UnitCost = parameterUnitCost.Value;
    myProductDetails.Description = (parameterDescription.Value).Trim();
    return myProductDetails;
  }

People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint