以上代码是更新数据库中的一行记录,经测试,没有报错但是数据库并没有的到更新,为什么?如果去掉this.hotel30DataSet.Tables["alinusers"].AcceptChanges();则报错。 
===================================================================================
不能先调用AcceptChanges() 再调用update() 应该反过来 先更新再AcceptChanges();去掉了会报错 报什么错呢?

解决方案 »

  1.   

    "当传递具有修改行的DataRow集合时,更新要求有效的UpdateCommand"错误
      

  2.   

    "当传递具有修改行的DataRow集合时,更新要求有效的UpdateCommand"错误
    =================================================================
    报的错误很明显是你的updatecommand错误
    之所以你的测试代码可以通过 那时因为测试代码是newrow那么update时调用的是insertCommand来处理
    而现在你是在做一个更新操作 所以报错了按你讲的方法试了一下,还是出现上述错误。
    =====================================
    应该反过来   先更新再AcceptChanges(); 这只是其中一个问题
      

  3.   

                  DBConnection DBC = new DBConnection();               SqlConnection thisConnection = DBC.ConnectSQLDB();
                   SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT * FROM alinusers", thisConnection);
                   SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
                   hotel30DataSet.Clear();
                   thisAdapter.Fill(hotel30DataSet, "alinusers");               string userPWD = "123456";               this.hotel30DataSet.Tables["alinusers"].Rows[index]["uname"] = addUserDlg.userCodeBox.Text;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["alevel"] = 0;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["upass"] = userPWD;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["describ"] = addUserDlg.userNameBox.Text;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["depart"] = addUserDlg.departcomboBox.Text;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["Rights"] = addUserDlg.functionRightBox.Text;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["login_right"] = addUserDlg.systemRightBox.Text;
                   this.hotel30DataSet.Tables["alinusers"].Rows[index]["reports_right"] = addUserDlg.reportRightBox.Text;               thisAdapter.Update(this.hotel30DataSet, "alinusers");               thisConnection.Close();用以上代码更新是没有问题的,但是是不是没有必要这么做?
    很明显,数据库已经连接上了,包括Adapter也已经定义了。
      

  4.   

    用以上代码更新是没有问题的,但是是不是没有必要这么做? 
    很明显,数据库已经连接上了,包括Adapter也已经定义了。
    ===============================================
    这个可以的代码 是使用sqlcommandbuilder来自动生成updatecommand的模式 不知道你指的没必要是什么意思,你不能更新的代码就是updatecommand错误 如果不使用sqlcommandbuilder来自动生成updatecommand,那么你需要自己来做这个工作.在指定了正确的updatecommand后 你就可以调用Adapter.Updat()方法来更新数据库了