ConnectionString属性尚未初始化

时间:2012-10-06 07:22:26

标签: vb.net

我正在研究班加罗尔大学规定的项目。我使用vb.net 2008作为前端,使用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

1 个答案:

答案 0 :(得分: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.