Now that you've established a connection to a database through a provider, you're ready to issue SQL commands. But how?
Solution
Use a Command object to issue SQL commands directly to your database through the provider connection.
Discussion
The following code updates a SQL Server table named Table1, changing every Column2 field to 25 whenever Column1 has a value of 0:
Solution
Use a Command object to issue SQL commands directly to your database through the provider connection.
Discussion
The following code updates a SQL Server table named Table1, changing every Column2 field to 25 whenever Column1 has a value of 0:
' ----- Connect to the database.
Dim connectionString As String = _
"Data Source=MySystem\SQLEXPRESS;" & _
"Initial Catalog=MyDatabase;Integrated Security=true"
Dim theDatabase As New SqlClient.SqlConnection(connectionString)
theDatabase.Open()
' ----- Prepare the SQL statement for use.
Dim sqlStatement As New SqlClient.SqlCommand( _
"UPDATE Table1 SET Column2 = 25 WHERE Column1 = 0", _
theDatabase)
sqlStatement.ExecuteNonQuery()
' ----- Clean up.
theDatabase.Close()
theDatabase.Dispose()