spacer
Yehuda Shiran October 9, 2002
Retrieving Categories 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

The simplest interaction with a MSSQL database is to retrieve a table from it with a stored procedure. To accomplish this task using JScript .NET, follow the recipe below:

  1. Create an instance of connection to the database, a SqlConnection object.
  2. Create an instance of a SQL command, a SqlCommand object.
  3. Mark the command as a stored procedure.
  4. Execute the command.
  5. Return the SqlDataReader result to the caller.

Here's an example. The ProductsDB.js file includes the method GetProductCategories(). As the name implies, this method retrieves the product categories from the database. It follows the recipe above, calling the stored procedure ProductCategoryList. This stored procedure is the simplest one, as it doesn't expect any parameters. It returns a table of all product categories in the databases. Here's 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;
  }

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