private void button6_Click(object sender, System.EventArgs e)
{  
 this.student_conn.Open();
  int pos=this.BindingContext[this.dsStudent1,"student"].Position;
  this.dsStudent1.Tables["student"].Rows[pos].Delete();
->this.sqldaStudent.Update(this.dsStudent1,"student"); 
  this.dsStudent1.Tables["student"].AcceptChanges(); 
  this.student_conn.Close();
}
在运行以上代码时会出现以下错误:
“未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
 其他信息: 系统错误。”
错误代码行是“->”所指向的行。
  我想进行单表的添加,删除和插入操作,我在一些资料上看到要想把对DataSet的修改应用到数据库就要使用SqlDataAdapter的Update方法,但是始终会出现上面的错误,请问在这里是不是应该使用Update方法来向数据库提交DataSet的更改?如果是那么Update方法应该如何使用?
如果有代码也可以贴上来或发到我的信箱:[email protected],谢谢.

解决方案 »

  1.   

    this.sqldaStudent.Update(this.dsStudent1,"student"); 
    改为
    this.sqldaStudent.Update(this.dsStudent1.GetChanges(),"student");
      

  2.   

    要Update方法更新数据,必须有以下条件:1.更新的表有且只有一个,2.表必须有一个主键看看SQL语句有无问题,是否包含主见字段
      

  3.   

    我的确是对单表进行的操作,这个表也有一个主键啊?!
    我的功能是这样:从DataGrid中点击一行,删除选中的那一行。我通过BindingContext[this.dsStudent1,"student"].Position得到选中行的行号,再通过dsStudent1.Tables["student"].Rows[pos].Delete()方法删除对应选中行号的那一行。而delete()只是从数据集中删除,并没有实际从数据库中删除,然后就用DataAdapter的Update方法把修改应用到数据库。这个过程中没有SQL语句啊?
      

  4.   

    要调用Update方法首先必须要先实现这个DataAdapter的.Selectcommand属性
      

  5.   

    是不是delete操作要先实现DeleteCommand,插入操作要先实现InsertCommand啊?
      

  6.   

    你是不是用向导做SqlDataAdapter的,如果是,那你看看最后一步“完成时各个命令是不是完全正确生成,dataset中多表也没问题,主要是表有主键
    建议你使用CurrencyManager cm=(CurrencyManager)this.BindingContext[this.dsStudent1,"student"]
    cm.REMOVEAT(cm.position);方便些
    一般用向导做且最后命令能正确生成的update应该没问题的
      

  7.   

    DataAdapter怎么可能不要sql语句就删除?正如你所说,Delete()没有真正到数据库,那到数据库用什么?如果要删除,必须要DataAdapter.Deletecommand,同样其他也是一样!
      

  8.   

    前面有这个语句没:
    SqlCommandBuilder  builder=new SqlCommandBuilder(SqlDataAdapter);
      

  9.   

    /// <summary>
    /// save button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_Save_Click(object sender, System.EventArgs e)
    {
    ComLogOutput.WriteLogFile("Button Save Click Begin"); if(!CheckUpdateMustInput())
    {
    return;
    }
    if(!CheckUpdateStyleInput())
    {
    return;
    } DataSet dsUpt = GetUpdateDataSet(); if(dsUpt.GetChanges() == null)
    {
    //lbl_Err.Text = "No data changed,please input again";
    lbl_Err.Text = ComLocalDiff.GetMessageById("0016",UserInfo.LangCode); }
    else
    {
    UpdateDataBase(dsUpt);
    } ComLogOutput.WriteLogFile("Button Save Click END");
    } /// <summary>
    /// update delete in db
    /// </summary>
    /// <param name="dstUpt"></param>
    private void UpdateDataBase(DataSet dsUpt)
    {
    try
    {
    string strSql = GetSearchSql(); //update data
    MstAreaDB mstDB = new MstAreaDB();
    mstDB.UpdateArea(dsUpt,strSql); //research data
    this.Session["dsp_MstArea_DgData"]=null;
    BindDataGrid(dg_MstArea.CurrentPageIndex); //ComJScript.AlertNormal(this,"Action complete sucessfully!");
    ComJScript.AlertNormal(this,ComLocalDiff.GetMessageById("0018",UserInfo.LangCode));
    }
    //Update Area
    public void UpdateArea(DataSet dsArea,string strSqlQuery)
    {
    ComDbInterface db = new ComDbInterface(); try
    {
    db.BeginTran();
    db.DataSetDBUpDate(strSqlQuery,dsArea);
    db.CommitTran();
    }
    catch(Exception err)
    {
    db.RollBackDb();
    throw err;
    }
    } protected void DataSetUpdateDB(string pSQL,DataSet resultData)
    {
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
    myDataAdapter.SelectCommand = new OleDbCommand(pSQL,CSystem.Connection,CSystem.Transaction);
    OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); resultData.Tables[0].TableName = "Table";
    myDataAdapter.Update(resultData);
    }
      

  10.   

    private void button6_Click(object sender, System.EventArgs e)
    {
     this.student_conn.Open();
    int pos=this.BindingContext[this.dsStudent1,"student"].Position;
    this.dsStudent1.Tables["student"].Rows[pos].Delete();
    ->this.sqldaStudent.Update(this.dsStudent1,"student");
    this.dsStudent1.Tables["student"].AcceptChanges(); 
    this.student_conn.Close();
    }
    对上面的代码修改,
    private void button6_Click(object sender, System.EventArgs e)
    {
     //this.student_conn.Open();//在单次更新下让sqlDataAdapter自己管理连接。
    DataRow dr =((DataRowView)this.BindingContext[this.dsStudent1,"student"]).Row;
    dr.Delete();
    this.sqldaStudent.Update(this.dsStudent1,"student");
    //this.dsStudent1.Tables["student"].AcceptChanges();//在调用SqlDataAdapter.Update后自动调用AcceptChanges 
    //this.student_conn.Close();
    }
    请确认你是否对SqlDataAdapter.DeleteCommand属性是否附于了实际删除数据的SqlCommand对象,
    可以参考,
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx