在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提供的類和方法。可以通過在項目中的“引用”文件夾上右鍵單擊,并選擇“添加引用”來添加所需的引用。