ARTICLE

Update 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 and update the existing records from the database.
Download Files:
 
Reader Level:

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.


tabl1.gif 

Table1.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


tabl2.gif

 

Now open the database and test it.


tabl3.gif

 

Table2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
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.
    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