vb.net 如何保存从 DataSet 到物理数据库的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16747574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:44:35  来源:igfitidea点击:

how to save changes from DataSet to physical Database

vb.netvisual-studio-2010dataset

提问by user2419810

I've been looking at this problem for a while and can't see what I'm doing wrong.

我一直在研究这个问题一段时间,但看不出我做错了什么。

I want to save changes from DataSet(ds.Tables("Login")) to physical Database (Data Source='|DataDirectory|\MyDatabase.accdb). But it doesn't work correctly. Data not saved in physical Database.

我想保存从 DataSet( ds.Tables("Login")) 到物理数据库 ( Data Source='|DataDirectory|\MyDatabase.accdb) 的更改。但它不能正常工作。数据未保存在物理数据库中。

Any Ideas how to solve it ??

任何想法如何解决它?

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles txtUsername.Click
    Dim ds As New DataSet()
    Dim dt As DataTable
    ds.Tables.Add(New DataTable("Login"))
    Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='|DataDirectory|\MyDatabase.accdb'"
    Dim sqlStr As String = "SELECT * FROM Login"
    Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
    dataAdapter.Fill(ds.Tables("Login"))

    dt = ds.Tables("Login")

    Dim dr As DataRow
    dr = dt.NewRow()
    dr("Username") = txtUserName.Text
    dr("Password") = txtPassword.Text

    dt.Rows.Add(dr)
    ds.AcceptChanges()
    dataAdapter.Update(ds.Tables("Login"))
End Sub

回答by Alan Macdonald

Look at this article and particularly step 7 which states you will need to set the UpdateCommand.

看看这篇文章,特别是第 7 步,它指出您需要设置 UpdateCommand。

https://support.microsoft.com/en-us/kb/301248

https://support.microsoft.com/en-us/kb/301248