ARTICLE

How to use command object in ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 10, 2011
This article defines how to use of command object to execute sql statements on database.
Download Files:
 
Reader Level:

Command object

This article defines how to insert data in database with the command object of ADO. The ADO command object is used to store and execute a single query against a database. The command object is used to execute select statements, insert, update, or delete statements, stored procedures, or any other statement which is defined by database. The .NET Framework Data Provider for SQL Server includes a SqlCommand object and oledb includes an OleDbCommand object.

Command object properties

The command object has the following properties.

CommandText - This property is used to return the command.

CommandType - This property is used to return the type of command.

Name - The name property is used to return a string that contains the name of a Command.

Creating a Command Object

To create a Sqlcommand object we can pass a query and connection object as a parameter.

Dim com As New SqlCommand("insert into emp3 values('ram','kumar')", con)

Now creating a table in Database and insert the value with the command object. like this

select * from emp3

OUTPUT

ta1.gif

Table1.gif

For example

The below example will insert the employee firstname and last name in the table emp3 from MS SQL server database.

Imports System.Data.SqlClient

Module Module1

    Sub Main()

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        con.Open()

        Dim com As New SqlCommand("insert into emp3 values('ram','kumar')", con)

        Console.WriteLine("Executing {0}", com.CommandText)

        Console.WriteLine("Number of rows affected : {0}", com.ExecuteNonQuery())

        Console.WriteLine("Type of the command : {0}", com.CommandType)

    End Sub

End Module

OUTPUT

ta2.gif 

Now open the database table and test it.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor