spacer
Yehuda Shiran October 13, 2002
The SqlCommand Class
Tips: October 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger