在VB中实现数据库功能,可以使用以下步骤:
Dim connectionString As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand()
command.CommandText = "SELECT column1, column2 FROM myTable"
command.Connection = connection
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
' 读取数据
Dim column1Value As String = reader("column1").ToString()
Dim column2Value As Integer = Convert.ToInt32(reader("column2"))
End While
reader.Close()
Dim command As New SqlCommand()
command.CommandText = "INSERT INTO myTable (column1, column2) VALUES (@value1, @value2)"
command.Connection = connection
command.Parameters.AddWithValue("@value1", value1)
command.Parameters.AddWithValue("@value2", value2)
command.ExecuteNonQuery()
connection.Close()
注意:在使用数据库功能时,需要先在项目中添加对ADO.NET的引用,以便使用ADO.NET提供的类和方法。可以通过在项目中的“引用”文件夹上右键单击,并选择“添加引用”来添加所需的引用。