0

我正在使用具有 10 个文本框、1 个注册按钮和 1 个退出按钮的表单。我在 MS Access 2007 中创建了数据库并将文件保存在桌面中。我已通过“添加新数据源”进行连接。提供者和路径是:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb。但是当我点击注册时,显示以下错误:

con.Open() - Doesn't have a valid file name.

这是我的代码:

Public Class Form2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb" & System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb"
    Dim insertcmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand
    Dim con As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection
    con.ConnectionString = constring
    insertcmd.CommandType = CommandType.Text
    insertcmd.CommandText = String.Format("INSERT INTO {0} VALUES ('{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}')", "Table1", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text, TextBox10.Text)
    insertcmd.Connection = con
    con.Open()
    Try
        insertcmd.ExecuteNonQuery()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try


    con.Close()
End Sub
End Class

请帮助解决这个问题。谢谢。

4

1 回答 1

0

您正在调用数据库字符串两次:

 Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\sipl\Desktop\Cust_Dtl.mdb" & System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb"

这给出了字符串“C:\Users\sipl\Desktop\Cust_Dtl.mdbC:\Users\sipl\Desktop\Cust_Dtl.mdb”

删除任何一个C:\Users\sipl\Desktop\Cust_Dtl.mdb"

或者& System.IO.Directory.GetCurrentDirectory & "Cust_Dtl.mdb

于 2013-04-21T10:11:02.467 回答