2012-10-06 179 views
1

我正在研究班加羅爾大學規定的項目。我使用vb.net 2008作爲前端,Ms Access 2007作爲後端。當我嘗試運行該程序時,出現錯誤ConnectionString屬性尚未初始化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

您要創建兩個連接的代碼,你設定的第一個起來,因此後者可能是一個複製和粘貼錯誤;

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. 
+0

so可以做些什麼先生? –

+0

@MadanRaj刪除指定的行'con = New OleDb.OleDbConnection',它不應該在那裏。 –

+0

那麼先生可以做些什麼?我正在理清這一點。 –

相關問題