ADO.NET command object in VB.NET

This article defines how to use of command object to execute sql statements on database.
  • 5846
 
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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.