SqlConnection thisConn = new SqlConnection("server=
CHINA-P82LHZ9IM;database=bank;uid=sa;pwd=hotechong");
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT * FROM bank",thisConn);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet,"Bank");
Console.WriteLine(thisDataSet.Tables["Bank"].Rows[4]["client"];
Console.WriteLine();
thisDataSet.Tables["Bank"].Rows[4]["client"] = "冯陈良";
Console.WriteLine(thisDataSet.Tables["Bank"].Rows[4]["client"]);
//上面一切正常
//可是加了下面这句就出现异常了
thisAdapter.Update(thisDataSet,"Bank");异常是
System.InvalidOperationException:当传递具有已修改行的DataRow集合时,更新要求有效的UpdateCommand

解决方案 »

  1.   

    在这句DataSet thisDataSet = new DataSet();后加:
    SqlCommandBuilder scb=new SqlCommandBuilder(thisDataSet);
      

  2.   

    你的thisAdapter只有Select命令,其它Update、Insert、Delete命令都没有设置上去。
      

  3.   

    在这句DataSet thisDataSet = new DataSet();后加:
    SqlCommandBuilder scb=new SqlCommandBuilder(thisDataSet);
    ///////////////////////////////////
    应该是SqlCommandBuilder scb=new SqlCommandBuilder(thisAdapter);
      

  4.   

    加了SqlCommandBuilder scb=new SqlCommandBuilder(thisAdapter);后现在异常变成System.InvalidOperationException:对于不返回任何键列信息的SelectCommand不支持UpdateCommand 的动态SQL生成。
      

  5.   

    在SqlCommandBuilder scb=new SqlCommandBuilder(thisAdapter);前面加上
    thisAdapter.SelectCommand = new SqlCommand(更新语句, thisConn);
      

  6.   

    指定thisAdapter.UpdateCommand
    thisAdapter.DeleteCommand,
    thisAdapter.InsertCommand,
    具体看例子,
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
    http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx