我写了一个如下方法:
                  /// <summary>
/// 更新DataSet中srcTable表
/// </summary>
/// <param name="sql">select语句</param>
/// <param name="ds">用于更新数据源的 DataSet</param>
/// <param name="srcTable">用于表映射的原表名称</param>
/// <returns></returns>
public void UpdateDataSet(string sql,DataSet dataSet,string srcTable)
{
try
{
OleDbConnection con = new OleDbConnection(conStr);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand(sql,con);
OleDbCommandBuilder oledbCB = new OleDbCommandBuilder(da);
da.Update(dataSet,srcTable);
}
catch(Exception err)
{
throw new Exception(err.Message);
}
}我先用一个select语句把表里的数据提出来了,放在DataGrid中,然后直接在DataGrid控件上修改,之后再调用上面的方法:如果如下:
 删除可以成功,
更新非空的字段也可以成功
增加新记录和更新空的字段就会失败,请问这是什么原因。所有操作都是直接在DataGrid控件上执行的。比如有一个表如下
Test
colA   colB   colC
1      aa     xxxx 
2      bb     tttt
3      cc  
4      dd     tttt先把这些数据提到一个DataSet中,再绑定到DataGrid中
然后在DataGrid控件上加一条新数据或直接在记录3的colC字段中添值,调用上面方法都都报错。
string sql = "SELECT * FROM Test"; DataSet ds = new DataSet();
DataTable dt = (DataTable)DataGrid.DataSource;

string srcTable = "Test";
int ret = RentalCommon.UpdateDataSet(sql,ds,srcTable); if(ret > 0)
{
MessageBox.Show("更新成功!");
ds.AcceptChanges();
}
else
{
MessageBox.Show("更新失败!");
ds.RejectChanges()
}

解决方案 »

  1.   

    报什么错?如果要用DataAdapter更新,删除,添加操作,必须要提供deletecommand,selectcommand,updatecommand,insertcommand,空字段出错可能是你数据库不允许插入空字段吧!
      

  2.   

    把你的错误写出来。用DataAdapter有时错误是很多的。
      

  3.   

    using System;
    using System.Drawing;
      

  4.   

    用这个方法可以,,,,这是我用的,,你可以借鉴string insertcmd,selectcmd,updatecmd,deletecmd;
    SqlCommand cmd;
    SqlParameter parm;
    SqlConnection conn = new SqlConnection(connstr);
    selectcmd = " select room_id as 机房名, rate as '费率(元/分钟)', computer_sum as 计算机总数 from room_info ";
    cmd = new SqlCommand(selectcmd,conn);
    adp.SelectCommand = cmd;
    if(dataGrid1.VisibleRowCount > count)
    {
    insertcmd ="INSERT INTO room_info(room_id, rate, computer_sum) VALUES (@room_id, @rate, @computer_sum); " +
    " select room_id as 机房名, rate as '费率(元/分钟)', computer_sum as 计算机总数 from room_info ";
    cmd = new SqlCommand(insertcmd,conn);
    cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    cmd.Parameters.Add("@rate", SqlDbType.Money,4, "费率(元/分钟)");
    cmd.Parameters.Add("@computer_sum",SqlDbType.TinyInt,1,"计算机总数");
    adp.InsertCommand = cmd;
    }
    else if(dataGrid1.VisibleRowCount == count)
    {
    updatecmd ="UPDATE room_info SET room_id = @room_id, rate = @rate, computer_sum = @computer_sum where room_id = @room_id ";
    cmd = new SqlCommand(updatecmd,conn);
    cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    cmd.Parameters.Add("@rate", SqlDbType.Money,4, "费率(元/分钟)");
    cmd.Parameters.Add("@computer_sum",SqlDbType.TinyInt,1,"计算机总数");
    adp.UpdateCommand = cmd;
    }
    else
    {
    deletecmd = "delete from room_info where room_id = @room_id";
    cmd = new SqlCommand(deletecmd,conn);
    parm = cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    parm.SourceVersion = DataRowVersion.Original ;
    adp.DeleteCommand = cmd;
    }
    adp.Update(dataset,"room_info");
    dataset.Clear();
    this.show();
      

  5.   

    如果要用DataAdapter更新,删除,添加操作,必须要提供deletecommand,selectcommand,updatecommand,insertcommand,
    估计是你没提供某个command吧
      

  6.   

    不好意思。这么久才回来。报的错是:UPDATE 语句的语法错误。我设置了表主键.
      

  7.   

    string insertcmd,selectcmd,updatecmd,deletecmd;
    SqlCommand cmd;
    SqlParameter parm;
    SqlConnection conn = new SqlConnection(connstr);
    selectcmd = " select room_id as 机房名, rate as '费率(元/分钟)', computer_sum as 计算机总数 from room_info ";
    cmd = new SqlCommand(selectcmd,conn);
    adp.SelectCommand = cmd;
    if(dataGrid1.VisibleRowCount > count)
    {
    insertcmd ="INSERT INTO room_info(room_id, rate, computer_sum) VALUES (@room_id, @rate, @computer_sum); " +
    " select room_id as 机房名, rate as '费率(元/分钟)', computer_sum as 计算机总数 from room_info ";
    cmd = new SqlCommand(insertcmd,conn);
    cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    cmd.Parameters.Add("@rate", SqlDbType.Money,4, "费率(元/分钟)");
    cmd.Parameters.Add("@computer_sum",SqlDbType.TinyInt,1,"计算机总数");
    adp.InsertCommand = cmd;
    }
    else if(dataGrid1.VisibleRowCount == count)
    {
    updatecmd ="UPDATE room_info SET room_id = @room_id, rate = @rate, computer_sum = @computer_sum where room_id = @room_id ";
    cmd = new SqlCommand(updatecmd,conn);
    cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    cmd.Parameters.Add("@rate", SqlDbType.Money,4, "费率(元/分钟)");
    cmd.Parameters.Add("@computer_sum",SqlDbType.TinyInt,1,"计算机总数");
    adp.UpdateCommand = cmd;
    }
    else
    {
    deletecmd = "delete from room_info where room_id = @room_id";
    cmd = new SqlCommand(deletecmd,conn);
    parm = cmd.Parameters.Add("@room_id", SqlDbType.VarChar, 8, "机房名");
    parm.SourceVersion = DataRowVersion.Original ;
    adp.DeleteCommand = cmd;
    }
    adp.Update(dataset,"room_info");
    dataset.Clear();
    this.show();-----------------------执行时报错:必须声明变量:@room_id
    ???????
      

  8.   

    建议不要使用CommandBuilder自动生成,
    完整例子,
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx