我用DataAdapter调用存储过程填充了一个DataSet,然后将DataSet里面的一张DataTable添加了几条记录,如何将新的DataTable更新到数据库? 
我用this._adapter.Update(ds,tableName);
结果报错:说当有新记录添加时,要求有效的InsertCommand,可我不知道如何写啊?
麻烦高手指点一下,最好能给几行代码,小弟感激不尽!

解决方案 »

  1.   

    我更新了啊
    obj=(DataSet)obj.GetChanges();
      

  2.   

    我昨天也遇到了这个问题     你试试这个看 
                //da 为SqlDataAdapter的对象(也就是你代码所说的"_adapter")
    SqlCommand command = new SqlCommand("insert into tablename values(@参数,@参数...)", conn);
                //写入对应的参数的值
                command.Parameters.Add("@参数",SqlDbType.数据类型);
                command.Parameters["@参数"].Value = 插入的值;
                command.Parameters.Add("@参数",SqlDbType.数据类型);
                command.Parameters["@参数"].Value = 插入的值;
                ..
                
                da.InsertCommand = command;
                //更新数据库
                da.Update(ds,"表名");
      

  3.   

    string modcmd="update Student set studentname=@studentname where studentno=@studentno";
    objSqlDataAdapter.UpdateCommand=new SqlCommand(modcmd,objSqlConnection);objSqlParameter=objSqlDataAdapter.UpdateCommand.Parameters.Add("@studentno",SqldbType.Int);
    objSqlParameter.SourceColumn="Studentno";
    objsqlparameter.SourceVersion=DataRowVersion.Original;objSqlParameter=objSqlDataAdapter.UpdateCommand.Parameters.Add("@studentname",SqldbType.Int);
    objSqlParameter.SourceColumn="Studentname";
    objsqlparameter.SourceVersion=DataRowVersion.current;objSqlDataAdapter.Update(objDataSet,"Student");
    刚才手写的,我找不到对不对。。共同学习吗。有问题就说列。。这好象可以的
      

  4.   

    上面的数据类型楼主要自己注意列。。我没在意列想@studentname,SqldbType.varchar
    才是对的,我上面有的int,所以是错的哦。。
      

  5.   

    问题已经解决了,我在前面填充一下就OK啦
    this._adapter.fill(ds,"tablename")