我必须改进我的编码才能从VB连接到数据库,但它仍然不会让它一直暗示“ConnectionString属性尚未初始化”。
MessageBox.Show(“已发送订单”)
provider = "Provider=Microsoft.Jet.OLEDB.12.0; Data Source="
dataFile = "J:\Database511_be.accdb"
myConnection.ConnectionString = connString
myConnection.Open()>>>>>THIS IS THE SECTION THAT BUGS ME, IT WOULDN'T CONNECT TO THE DATABASE
Dim str As String
str = "Insert INTO CUSTOMER_DATABASE([Items],[Pizza Size],[Quantity],[Table Number], [Total]) Values ( ?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Items", CType(TextBox3.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Pizza Size", CType(TextBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Quantity", CType(TextBox5.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Table Number", CType(ListBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Total", CType(TextBox8.Text, String)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox8.Clear()
Catch ex As System.Exception
End Try
答案 0 :(得分:1)
您正在定义连接字符串,但未将其与OleDbConnection对象关联。你想这样做:
Using connection As New OleDbConnection(connString)
connection.Open()
' ... do stuff with the open connection
End Using
或者,甚至更好
ApplicationConfig