使用DataRow C#更新存在的数据

时间:2009-08-04 08:33:48

标签: mysql datarow

我需要在mysql数据库中更新我现有的数据。

我写的就是这段代码;

        String _id = lbID.Text;
        dsrm_usersTableAdapters.rm_usersTableAdapter _t = new dsrm_usersTableAdapters.rm_usersTableAdapter();
        dsrm_users _mds = new dsrm_users();
        _mds.EnforceConstraints = false;

        dsrm_users.rm_usersDataTable _m = _mds.rm_users;

        _t.FillBy4(_m, _id);
        if(_m.Rows.Count >0 )
        {

            DataRow _row = _m.Rows[0];

             _row.BeginEdit();

            _row["username"] = txtUserName.Text;

            _row.EndEdit();

            _row.AcceptChanges();

            _t.Update(_m);

        }

但是没有任何改变我存在的数据。问题是什么?

2 个答案:

答案 0 :(得分:1)

我认为问题是你在调用DbDataAdapter.Update()之前调用DataRow.AcceptChanges()。 AcceptChanges会将数据行的状态设置为“orignal”(或“未更改” - 我现在不记得)。尝试在更新后将AcceptChanges的调用移到。

答案 1 :(得分:-2)

在传递带有修改行的DataRow集合时,更新需要有效的UpdateCommand

是的,我在更新后移动AccesptChange()现在它给出了这个错误

在传递带有修改行的DataRow集合时,更新需要有效的UpdateCommand

但是现在的问题是,我使用MySQL并且我不能Wrie UpdateCommand,VS2008不接受SQL命令。自动删除所有SQL命令。我不明白这个问题。那你现在用另一种方法不使用SQL命令(UpdateCommand)吗?