Updating Data ado.net tutorials

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes how to update data in a database

You frequently need to modify existing rows of data in a table using the UPDATE statement. When coding UPDATE statements, you must be careful to include a WHERE clause, because if you miss a WHERE clause, then you will change all the records of the table.

The Syntax of the Update Statement is as follows:

    UPDATE table-name SET column = expression [WHERE selection-criteria]

The following code shows you how to add a single row to a table:

    Dim ConnectionString As String = _
    "Data Source=SqlServerNameOrIP;Initial Catalog=DBName;Integrated Security=True"
    Dim connection As New SqlConnection(ConnectionString)

      ' declare and create a sql command object
      Dim command As SqlCommand = connection.CreateCommand

      ' set the sql statement to execute at the data source
      command.CommandText = _
      "UPDATE Users SET Password = @Password WHERE Username = @Username"

      ' add the parameters
      command.Parameters.AddWithValue("@Username", Username.Text)
      command.Parameters.AddWithValue("@Password", Password.Text)

      ' execute the sql statement against the connection

      ' release the resources
    Catch ex As Exception
      ' close the connection
    End Try

Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2018 - All Rights Reserved.