3
Answers

why do Update method of SqldataAdapter not work?

Ask a question
Bruce Lam

Bruce Lam

15y
3.6k
1
This is my coding, DeleteComand and InsertComand work, but UpdateComand not work, if change dap.AcceptChangesDuringFill is true, then UpdateComand work, but Delete and Insert not work,   please help me to solve this problem as impossible as
         
            DataSet dsLocal = new DataSet();
            DataSet dsServer = scsiClass.getDS("SELECT * FROM [t_HR_Group]", "t_HR_Group");

            SqlDataAdapter dap = new SqlDataAdapter("SELECT * FROM [t_HR_Group1]", con);
            dap.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            dap.AcceptChangesDuringFill = false;

            dap.UpdateCommand = cmd.GetUpdateCommand();
            dap.DeleteCommand = cmd.GetDeleteCommand();
            dap.InsertCommand = cmd.GetInsertCommand();

            dap.Fill(dsLocal, "t_HR_Group");
            SqlCommandBuilder cmd = new SqlCommandBuilder(dap);

            dsServer.Merge(dsLocal, true, MissingSchemaAction.AddWithKey);
            //dsServer.GetChanges();

            int cn = dsServer.Tables[0].Rows.Count - 1;
            for (int i = 0; i <= cn; i++)
            {
                DataRow dr = dsServer.Tables[0].Rows[i];
                if (dr.RowState == DataRowState.Unchanged)
                {
                    dr.SetAdded();
                }
                else
                    if (dr.RowState == DataRowState.Added)
                    {
                        dr.AcceptChanges();
                        dr.Delete();
                    }
            }
          
                     
            int eff=dap.Update(dsServer.Tables[0]);

Answers (3)