我不知道我怎么能在程序中修正错误...问题是con.open()
Private Sub btnadd1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnadd1.Click
Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable
If Not con.State = ConnectionState.Open Then
con.Open()
cmd.Connection = con
End If
If Me.text1.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO Datagrid1(StickerCode, Description, Company, Department, Location, User, SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model ) " & _
" VALUES(" & Me.text1.Text & ",'" & Me.text2.Text & "','" & _
Me.text3.Text & "','" & Me.text4.Text & "','" & Me.text5.Text & "','" & _
Me.text6.Text & "','" & Me.text7.Text & "','" & Me.text8.Text & "','" & _
Me.text9.Text & "','" & Me.text10.Text & "','" & Me.text11.Text & "','" & _
Me.text12.Text & "')"
cmd = New OleDbCommand(cmd.CommandText, con)
cmd.ExecuteNonQuery()
Else
cmd.CommandText = "UPDATE Form4 " & _
" SET StickerCode='" & Me.text1.Text & _
", Description='" & Me.text2.Text & "'" & _
", Company='" & Me.text3.Text & "'" & _
", Department='" & Me.text4.Text & "'" & _
", Location='" & Me.text5.Text & "'" & _
", User='" & Me.text6.Text & "'" & _
", SerialNumber='" & Me.text7.Text & "'" & _
", DatePurchased='" & Me.text8.Text & "'" & _
", Tagable='" & Me.text9.Text & "'" & _
", Quantity='" & Me.text10.Text & "'" & _
", Brand='" & Me.text11.Text & "'" & _
", Model='" & Me.text12.Text & "'" & _
" WHERE text1=" & Me.text1.Tag
cmd.ExecuteNonQuery()
End If
RefreshData()
Me.btnclear1.PerformClick()
con.Close()
End Sub
答案 0 :(得分:1)
您还没有将连接字符串传递给您的连接对象,因此它不知道要打开哪个数据库或在哪里找到它。
试试这个:
Dim con As New OleDb.OleDbConnection("My connection string")
答案 1 :(得分:1)
您没有为OleDbConnection
对象提供连接字符串。连接字符串告诉OleDbConnection
对象您希望它连接到哪个数据库。您可以在构造函数中为它提供连接字符串,如下所示:
Dim con As New OleDb.OleDbConnection(connectionString)
或者,您可以通过ConnectionString
属性设置它,如下所示:
Dim con As New OleDb.OleDbConnection()
con.ConnectionString = connectionString
实际连接字符串的格式因数据库引擎和版本而异。但是,网上有大量资源,它们会显示正确的格式以及它可以包含的选项。举个例子,它可能看起来像这样:
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"
很抱歉,如果我通过讨论乱序的步骤让你感到困惑。基本上,您需要做的就是将连接字符串赋予OleDbConnection
对象。在调用Open
方法之前,您需要这样做。例如,您可以更改以下行:
Dim con As New OleDb.OleDbConnection
对此:
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb")
但是,显然,您需要将连接字符串更改为适合您特定情况的任何内容。上面的例子只是有效连接字符串的示例。
或者,如果您不想立即将连接字符串提供给OleDbConnection
对象,您可以稍后再将其提供给它,只要您在调用Open
之前设置它即可。 。例如,您可以更改以下行:
Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable
If Not con.State = ConnectionState.Open Then
con.Open()
' ...
对此:
Dim cmd As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection
Dim Printlist1 As New DataTable
If Not con.State = ConnectionState.Open Then
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"
con.Open()
' ...
原始示例似乎让您感到困惑,首先将连接字符串存储在变量中,然后将该变量中的值传递给连接对象,如下所示:
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\MyDb.mdb"
Dim con As New OleDb.OleDbConnection(connectionString)