2013-06-20 220 views
1

當我按下按鈕時,我希望將Sname和SNo寫入數據庫。這裏是我的代碼:無法寫入訪問數據庫

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\cerns1\Local Settings\Application Data\Temporary Projects\WindowsApplication1\testdb.accdb" 
    Dim cnn As New OleDbConnection(ConnString) 

    ' Create a new row. 
    Dim newStudentRow As testdbDataSet.StudentRow 
    newStudentRow = Me.TestdbDataSet.Student.NewStudentRow() 


    ' Save the new row to the database 
    Dim i As Integer = 2 
    Dim SNo As Integer = 1 
    Do While i > 1 
     cnn.Open() 
     'StudentTableAdapter.Insert("WOW", SNo) 
     Me.TestdbDataSet.Student.Rows.Add(newStudentRow) 
     Me.StudentTableAdapter.Update(Me.TestdbDataSet.Student) 
     SNo = SNo + 1 
     i = i + 1 
     cnn.Close() 
    Loop 


End Sub 

執行後,沒有信息在Access數據庫中添加或更改。

+0

那麼,這是什麼問題? –

+0

只是一個提示:這段代碼會讓你的程序崩潰。只要我大於1,循環就會進行,並且每次迭代時你都將1加1。在每次迭代中打開和關閉連接都不好,打開一次並在完成操作後關閉它。 – Abbas

+0

@ Bill Gregg它會毫無問題地運行,但不會保存到訪問數據庫。當我打開數據庫後仍然是一樣的。 –

回答

1

我發現,如果我查看調試文件夾中的更改保存在那裏。要改變,我必須手動將連接字符串更改爲我的WindowsApplication1文件夾。