How do you execute the SQL command in VB?
You can execute SQL commands in VB using ADO.NET. Here is a simple example:
First, it is necessary to introduce the relevant namespace.
Imports System.Data.SqlClient
Next, create a SqlConnection object to connect to the database.
Dim connectionString As String = "Your Connection String"
Dim connection As New SqlConnection(connectionString)
Next, create a SqlCommand object and specify the SQL command to be executed.
Dim sql As String = "SELECT * FROM YourTable"
Dim command As New SqlCommand(sql, connection)
Next, open the database connection.
connection.Open()
Next, execute the SQL command and retrieve the results.
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
' 读取数据
Dim column1 As String = reader.GetString(0)
Dim column2 As Integer = reader.GetInt32(1)
' ...
End While
reader.Close()
Finally, close the database connection.
connection.Close()
Please note that the code above is just an example, you will need to make appropriate modifications and adjustments based on your actual situation. Additionally, there are other methods available to execute different types of SQL commands, such as using ExecuteNonQuery for INSERT, UPDATE, or DELETE statements, and ExecuteScalar for executing SQL commands that return a single value.