对DataGrid的更新删除代码如下
总是出现一个问题,第一次更新删除就可以成功,第二次更新删除都失败错误提示是:追加信息:同时实行违反: UpdateCommand 被处理0件。我想是不是Datagrid的数据没有更新啊。
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.ToolTipText=="Save")
{
// try
// {
int row =this.dataGrid2.CurrentCell.RowNumber;
this.dataGrid2.CurrentCell=new DataGridCell(row+1,0);
if(this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("数据修改成功!","信息");

}
else
{
MessageBox.Show("没有修改数据!");
return;
}
// }
// catch
// {
// MessageBox.Show("数据保存失?,???所有信息?入完整且正?!","提示");
// return;
// }
}
if(e.Button.ToolTipText=="Delete")
{
if(MessageBox.Show("??要?除?条????","??",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
int intRowNumber=this.dataGrid2.CurrentCell.RowNumber;
try
{
this.ds.Tables[0].Rows[intRowNumber].Delete();
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("数据?除成功!","信息");
}
catch
{
MessageBox.Show("?数据不能?除!","提示");
return;
}
}
}
}

解决方案 »

  1.   

    LinkDataBase  类using System;
    using System.Data;
    using System.Data.SqlClient;namespace PDA
    {
    /// <summary>
    /// LinkDataBase 
    /// </summary>
    public class LinkDataBase
    {
    private string strSQL;
    private string connectionString = "server=*****;uid=******;pwd=******;database=*******";
    private SqlConnection myConnection;

    private SqlCommandBuilder sqlCmdBld;
    private DataSet ds = new DataSet();
    private SqlDataAdapter da; public LinkDataBase()
    {
    //
    // TODO
    //
    } ///////////////////////////////// /////////////////////////////////////////////////////
    public DataSet SelectDataBase(string tempStrSQL,string tempTableName)

    this.strSQL = tempStrSQL;
    this.myConnection = new SqlConnection(connectionString);
    this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
    this.ds.Clear();
    this.da.Fill(ds,tempTableName);
    return ds;
    }
    public DataSet UpdateDataBase(DataSet changedDataSet,string tableName)
    {
    this.myConnection = new SqlConnection(connectionString);
    this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
    this.sqlCmdBld = new SqlCommandBuilder(da);
    this.da.Update(changedDataSet,tableName);
    return changedDataSet;
    } ///////////////////////////////// /////////////////////////////////////////////////////
    public DataTable SelectDataBase(string tempStrSQL)
    {
    this.myConnection = new SqlConnection(connectionString);
    DataSet tempDataSet = new DataSet();
    this.da = new SqlDataAdapter(tempStrSQL,this.myConnection);
    this.da.Fill(tempDataSet);
    return tempDataSet.Tables[0];
    }
    public int UpdateDataBase(string tempStrSQL)
    {
    this.myConnection = new SqlConnection(connectionString); myConnection.Open();
    SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL,this.myConnection);
    int intNumber = tempSqlCommand.ExecuteNonQuery();
    myConnection.Close();
    return intNumber;
    }
    }
    }