1

我正在研究班加罗尔大学规定的一个项目。我使用 vb.net 2008 作为前端,使用 Ms Access 2007 作为后端。当我尝试运行该程序时,我收到一个错误,因为ConnectionString 属性尚未初始化

这是我使用的代码

private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
    Dim con As New OleDb.OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "DataSource=C:\My Project\project.mdb"
    con.ConnectionString = dbProvider & dbSource
    con = New OleDb.OleDbConnection

    con.Open()
    cmd.Connection = con
    cmd.CommandText = "Insert Into Application(appno, mname, age, gender, height, weight, address, purpose, bmi, mailid, phoneno, paymnet)" & _
                      "Values('" & Me.appno.Text & "', '" & Me.mname.Text & "', '" & Me.age.Text & "', '" & Me.gender.Text & "', '" & _
                      Me.mheight.Text & "', '" & Me.weight.Text & "', '" & Me.address.Text & "', '" & Me.purpose.Text & "', '" & _
                      Me.bmi.Text & "', '" & Me.mailid.Text & "', '" & Me.phoneno.Text & "', '" & Me.payment.Text & "')"
    cmd.ExecuteNonQuery()
    con.Close()
End Sub
4

1 回答 1

1

您正在创建两个连接,您正在设置第一个连接,因此后一个连接可能是复制和粘贴错误;

Dim con As New OleDb.OleDbConnection

...

dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dbSource = "DataSource=C:\My Project\project.mdb"
con.ConnectionString = dbProvider & dbSource
con = New OleDb.OleDbConnection                  <-- creates a new connection
                                                     without a connection string.
于 2012-10-06T07:30:59.407 回答