inserting new record in the msaccess using dataset
I am trying to insert a new record into msaccess2003 mdb file my version is VB.net 2005, it doesnt show any error and also when i open my access db file, there is no record inserted, And also how can i format the date field it is giving me error, how can i convert the txtdate.text to date compatible with ms acess
here is the code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 开发者_StackOverflow社区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
ok well, I can give you a hint to start ( even though this is an old thread ) I wouldve find this usefull if I had stumble into this when I started my projet a month ago.
what you have done here is you created a new row virtually, and add it to your virtual database ( the dataset )
the acceptchanges method only change the relation of your row vs your dataset, it will commit the change so your dataset will consider the row not as a newly created row, but as a regular row.
what you want here is to open a connection, use an Update command on a dataAdapter, so it ( the DA) will look at all the changes in your dataset and commit them to the actual database.
it took me a while to get it working, most if it was testing and error and reading tons of blog/internet info.
http://msdn.microsoft.com/en-us/library/6264xxbd.aspx
start here and once you understand the DA you'll be very close to your answer.
精彩评论