adapter.InsertCommand = cmd;
你的adapter执行Updata时调用InsertCommand里的sql语句,而你这个InsertCommand里是select语句,肯定不能更新数据库必须是updata语句。
如:
catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " +
                                     "WHERE CategoryID = @CategoryID" , nwindConn);catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original;

解决方案 »

  1.   

    adapter.InsertCommand = cmd;
    这里的问题了
      

  2.   

    不用楼上的方法了,还是使用SqlCommandBuilder来自动生成好
      

  3.   

    谢谢大家,问完这个问题马上散分。
    SqlCommand addcmd = new SqlCommand("Insert into base(name,age) values(@name,@age)",conn);
    addcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.NVarChar, 10, "name"));
    addcmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@age", System.Data.SqlDbType.NVarChar, 10, "age"));adapter.InsertCommand = addcmd;
    DataRow drAdd = ds.Tables["table1"].NewRow();
    drAdd[1] = dg[dg.CurrentRowIndex,1];
    drAdd[2] = dg[dg.CurrentRowIndex,2];
    ds.Tables["table1"].Rows.Add(drAdd);
    //////////
    adapter.Update(ds,"base");为什么会产生两条同样的记录呀????
    DataRow drAdd = ds.Tables["table1"].NewRow();
    drAdd[1] = dg[dg.CurrentRowIndex,1];
    drAdd[2] = dg[dg.CurrentRowIndex,2];
    ds.Tables["table1"].Rows.Add(drAdd);
    以上语句必须留下,请问怎么修改才能实现??
      

  4.   

    楼上的问题出在InsertCommand里的查询语句。
    用SqlCommandBuilder效率比较低,最好还是自己写
    dataAdapter.InsertCommand.CommandText = "Sql语句";
    如果InsertCommand不存在,就要实例化一个SqlCommand对象。
    dataAdapter.InsertCommand = new SqlCommand();