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.
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
Now open the database and test it.