ARTICLE

Delete command in ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 11, 2011
Here we see how to use ADO.net to connect to a SQL Server database, count the records and delete the records from the database.
Download Files:
 
Reader Level:

Here we see how to use ADO.net to connect to a SQL Server database, count the records and delete the records from the database. we create a table in SQL Server database which has the name emp3 and using count statement to count the row and delete command delete the records from table.

Creating connection object

To create a connection we pass the connection string as a parameter in connection object.

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

Dim con As New SqlConnection(str)

 

Count rows in a table

To count all the rows of the table we use the following statement.

select count(*) from emp3

 

Delete command

 

The delete command is used to delete the record from database has the below command:

delete from emp3 where firstname='rahul' AND lastname='kumar'

 

Now we create a database table and insert some values in this table. Table looks like this.


tab1.gif 

Table1.gif

 

For Example

 

The below example defines the count command and delete command.

 

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)

        Try

            con.Open()

            Dim com As New SqlCommand("select count(*) from emp3", con)

            Console.WriteLine("Number of row in table:=" & com.ExecuteScalar())

            com.CommandText = "delete from emp3 where firstname='rahul' AND lastname='kumar'"

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

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

            con.Close()

        Catch ex As Exception

            Console.WriteLine("can not delete record")

        End Try

    End Sub

End Module

 

OUTPUT


tab2.gif


Now open the database and test it.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • 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. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor