how to save the DataSet after making changes to the database?

风格不统一 提交于 2019-12-23 01:35:10

问题


if I have a DataSet called myDs and I edit a field in it by direct access in a loop like the following:

for (int i = 0; i < myDS.Tables[TableName].Rows.Count; i++)
{

    //some function or web method to get the id value of the record being updated
    int n = getNewNumber();

    //updating the dataset record according to some condition
    if (n == 0)
    {
        myDS.Tables[TableName].Rows[i]["id"] = n;
        myDS.Tables[TableName].Rows[i]["description"] = "some data";
    }
    else
    {
        myDS.Tables[TableName].Rows[i]["id"] = n;
        myDS.Tables[TableName].Rows[i]["description"] = "new data";
    }
}

How I make these changes done in the database as I could see it in the GridView when I do databind() but the database is not affected and I try using the fill & update methods of OdbcDataAdapter and OdbcCommandBuilder?


回答1:


Try the TableAdapter.Update process shown in this article: How to: Update Records in a Database.

Also, please make sure you not only have the necessary access to the database you are trying to connect to, but also permission to update records in the desired table. You may have a SQL Server configuration problem that is preventing your code from updating.




回答2:


Since it is odbc, you might use an OdbcDataAdapter; then call:

adapter.Update(myDS.Tables[TableName]);

disclaimer: personally, I never use DataSet for.... anything. YMMV.




回答3:


You should type two line after loop

myDS.AcceptChanges();
adapter.Update(myDS.Tables[TableName]);


来源:https://stackoverflow.com/questions/7055799/how-to-save-the-dataset-after-making-changes-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!