ADO.NET Update command in VB.NET

Here we see how to use ADO.net to connect to a SQL Server database and update the existing records from the database.
  • 4861
Download Files:
 

Here we see how to use ADO.net to connect to a SQL Server database and update the existing records from the database. we create a table in SQL Server database which has the name personaltab and using update command to update the records in table.

Creating connection

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)


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

create-datababe-table-in-vb.net.gif

Update command


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

update personaltab set Firstname='monu', Lastname='rathor' where City='delhi' and P_Id='1'

 

For example

 

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("update personaltab set Firstname='monu', Lastname='rathor' where City='delhi' and P_Id='1'", con)

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

            com.ExecuteNonQuery()

            Console.WriteLine("update has been completed")

            con.Close()

        Catch ex As Exception

            Console.WriteLine("can not update record")

        End Try

    End Sub

End Module

 

OUTPUT

 

vb.net-command-prompt.gif

 

Now open the database and test it.
 

test-database-in-vb.net.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.