spacer
Yehuda Shiran October 13, 2002
The SqlCommand Class
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

IBuySpy Store uses stored procedures to interact with the MSSQL database. Use the SqlCommand class to assemble an instance object with all the information required by the stored procedure. This class cannot be inherited. When created, the SqlCommand object points to the beginning of the database, for both read and write. This class includes four methods: ExecuteReader(), ExecuteNonQuery(), ExecuteScalar(), and ExecuteXmlReader().

ExecuteReader() executes stored procedures that return rows from the database. They are usually stored in an SqlDataReader object. The method GetProductCategories() uses ExecuteReader() to execute the stored procedure ProductCategoryList.

  public function GetProductCategories() : SqlDataReader {
    var myConnection : SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    var myCommand : SqlCommand = new SqlCommand("ProductCategoryList", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myConnection.Open();
    var result : SqlDataReader  = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    return result;
  }
ExecuteNonQuery() executes SQL queries such as INSERT, DELETE, UPDATE, and SET. Run ExecuteNonQuery() when you pass back information from the stored procedure through parameters as opposed to records. The GetProductDetail() method uses ExecuteNonQuery():

  .
  .
  myConnection.Open();
  myCommand.ExecuteNonQuery();
  myConnection.Close();
  .
  .
ExecuteScalar() retrieves a single value (for example, an aggregate value) from a database. ExecuteXmlReader() sends the CommandText to the Connection and builds an XmlReader object. The latter two methods are not used in IBuySpy.


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