Using a filter - WHERE
By using a filter (WHERE), we can specify which fields of which records to update. Once again, there is no data retrieved from the database as part of the operation. For this reason, we do not need to use a data adapter or dataset.
UPDATE Customers SET FirstName=@FirstName,LastName=@LastName WHERE CustomerID=@CustomerID
Updates the specified fields of the a customer record matching the parameter of CustomerID.
BE CAREFUL
If you omit the filter, you will update all records, as below:
UPDATE Customers SET FirstName=@FirstName,LastName=@LastName
Updates all of the specified fields of all of the customer records.
Note the following code sample:
Public Sub updateCartQuantityPlusOne(CartID As Integer)
Dim myConn As SqlConnection = New SqlConnection(MyConnString)
myConn.Open()
Dim strSQL As String = "UPDATE XYZCart SET Quantity = Quantity + 1 WHERE CartID = @CartID"
Dim myComm As SqlCommand = New SqlCommand(strSQL, myConn)
myComm.Parameters.AddWithValue("@CartID", CartID)
myComm.ExecuteNonQuery()
myConn.Close()
End Sub
Once again, parameters are used to pass the data to update the record and the ExecuteNonQuery method of the command object is engaged to complete the operation.
No comments:
Post a Comment