Database Code
  Home arrow Database Code arrow A Closer Look at ADO.NET: The Command Obje...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE CODE

A Closer Look at ADO.NET: The Command Object
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 45
    2005-08-02

    Table of Contents:
  • A Closer Look at ADO.NET: The Command Object
  • The ExecuteScalar() Method
  • The ExecuteNonQuery() Method (INSERT Data)
  • The ExecuteNonQuery() Method (DELETE Data)
  • Creating and Executing a Stored Procedure

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Closer Look at ADO.NET: The Command Object


    (Page 1 of 5 )

    In this article, Michael Youssef shows us how to use the Command Object to execute SQL commands on the database server. He details commands we need to perform on the database like INSERT, UPDATE, DELETE and SELECT, represented in ADO.NET as Command Object. You should already know how to create a connection with the database, or you can refer to "A Closer Look at ADO.NET: The Connection Object" to learn how.

    As with the connection object, there are provider-specific classes for the command object.  For example, the SQL Server Provider has the class SqlCommand and the OLEDB .NET Data Provider has the class OleDbCommand. The Command Classes implement the IDbCommand Interface, which has methods like ExecuteReader(), ExecuteNonQuery(), ExecuteScalar(), CreateParameter(), Cancel() and Prepare(). The interface also has the following properties: Connection, CommandTimeout, CommandType, CommandText, Parameters and Transaction. We will be discussing all these properties and methods, so don't worry about that.

    You can create a command object in many ways. You can create a connection object, then use the method CreateCommand() of the connection object to create a Command Object. Or you can create a connection object, then create a command object using the new operator (the standard way). After that, assign the connection object reference to the Connection property of the command object.

    The command object has a property called CommandText, which contains a string value that represents the command that will be executed in the database server. So to use the command object, you must associate it with a connection object (which must be open before you use the command Object). Then assign the command string value to the CommandText property and use of the Execute methods.

    The CommandType property takes a value of the System.Data.CommandType Enumeration (CommandType.Text, CommandType.TableDirect or CommandType.StoredProcedure). ADO.NET recognizes three types of commands. The first type is text commands, which are those text commands that are sent to the database server directly and written in specific SQL Dialect (for SQL Server, MSDE and Access it's T-SQL). The commandType.Text is the default for the property CommandType of the Command object.

    The second type is the CommandType.StoredProcedure which is set to call a specific stored procedure in the database; of course when you set the Command.CommandType property to CommandType.StoredProcedure, the value of the Command.CommandText must be the name of the stored procedure that you are calling. Finally, the type CommandType.TableDirect is used to return a complete table from the database, and it's used only by the OLEDB .NET Data Provider. I think that it's time to create a command object and execute a T-SQL Command in the database.

    More Database Code Articles
    More By Michael Youssef


       · Its the tutorial I've been looking for. Awesome tutorial. Everything Nicely...
       · Hello, Thanks and I hope that you like my C# articles too :-). About ADO.NET...
     

    DATABASE CODE ARTICLES

    - Deployment of the MobiLink Synchronization M...
    - MobiLink Synchronization Wizard in SQL Anywh...
    - Finding Matching Records in Data Access Pages
    - Using the AccessDataSource Control in VS 2005
    - A Closer Look at ADO.NET: The Command Object
    - A Closer Look at ADO.NET: The Connection Obj...
    - Using ADO to Communicate with the Database, ...
    - Code Snippets: Counting Records
    - Constraints In Microsoft SQL Server 2000
    - Multilingual entries into a DB and to be dis...
    - Getting A List of Tables From SQL Server
    - SQL Server Database Creator - .NET Version
    - ADO Recordset Paging
    - Two combos, one textbox example
    - Discussion & Listserv Module by Mike Eck...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT