删除dataset中相关表的记录,只需要在这些表之间建立关系就可以了。

解决方案 »

  1.   

    先把DataGrid绑定:
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
    conn.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
    dt = new System.Data.DataSet();
    da.Fill(dt,"customers");
    this.DataGrid.SetDataBinding(dt,"customers");删除的代码用如下:
    this.BindingContext[dt,"customers"].RemoveAt(this.BindingContext[dt,"customers"].Position);更新的语句用如下 :
    this.sqlDeleteCommand1.CommandText = @"DELETE FROM YZ_STAMPER WHERE (StamperID = @Original_StamperID) AND (EmployeeID = @Original_EmployeeID OR @Original_EmployeeID IS NULL AND EmployeeID IS NULL) AND (StamperName = @Original_StamperName OR @Original_StamperName IS NULL AND StamperName IS NULL) AND (State = @Original_State OR @Original_State IS NULL AND State IS NULL)";
    this.sqlDeleteCommand1.Connection = this.conn;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_StamperID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "StamperID", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_EmployeeID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_StamperName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "StamperName", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_State", System.Data.SqlDbType.VarChar, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "State", System.Data.DataRowVersion.Original, null));
    //  conn.Open();

    sqlDa.UpdateCommand = sqlUpdateCommand1;
    sqlDa.InsertCommand = sqlInsertCommand1;
    sqlDa.DeleteCommand = sqlDeleteCommand1;
    System.Data.SqlClient.SqlTransaction myTran = conn.BeginTransaction();
    sqlUpdateCommand1.Transaction = myTran;
    sqlInsertCommand1.Transaction = myTran;
    sqlDeleteCommand1.Transaction = myTran;
    try
    {
    sqlDa.Update(dt,xTableName); 
    myTran.Commit();
    return true;
    }
    catch(System.Data.SqlClient.SqlException ex)
    {
    myTran.Rollback();
    return false;

    finally
    {
    conn.Close();
    }
    }