2011-05-17 61 views
0

我想插入一個新的記錄到msaccess2003 mdb文件我的版本是VB.net 2005,它不顯示任何錯誤,也當我打開我的訪問數據庫文件時,沒有記錄插入的,也是我如何格式化日期字段它給我的錯誤,我怎麼能txtdate.text轉換爲最新與MS兼容接取使用數據集在msaccess中插入新記錄

這裏是代碼

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    Dim ds As New DataSet 
    Dim db As New ClassDB 

    ds = db.FetchData("ricemill.mdb", "sales") 


    Dim anyRow As DataRow = ds.Tables(0).NewRow 
    Dim mydate As DateTime 
    Dim cmydate As String 

    cmydate = txtdate.Text 
    mydate = DateTime.Parse(mydate) 


    anyRow("description") = txtdesc.Text 
    anyRow("date") = DateTime.Parse(txtdate.Text) 
    anyRow("amount") = txtamount.Text 



    ds.Tables(0).Rows.Add(anyRow) 
    ds.Tables(0).AcceptChanges() 
    ds.AcceptChanges() 

    MsgBox("Record Added ! ") 

End Sub 

回答

1

確定好,我可以給你一個啓動提示(即使這是一箇舊線程),如果我在一個月前開始我的projet時遇到這種情況,我會發現這個有用的。

你在這裏做什麼是您創建一個新的行實際上,並將其添加到您的虛擬數據庫(數據集)

AcceptChanges方法只能改變你行VS數據集之間的關係,將提交更改,以便您的數據集將行視爲不是新創建的行,而是作爲常規行。

你想在這裏打開一個連接,在dataAdapter上使用Update命令,所以它(DA)將查看數據集中的所有更改並將它們提交給實際的數據庫。

我花了一段時間纔得到它的工作,大部分是如果它測試和錯誤和讀博客/互聯網信息噸。

http://msdn.microsoft.com/en-us/library/6264xxbd.aspx

這裏開始,一旦你瞭解DA你會很接近你的答案。