关于DataSet.merge 问题的答案。

代码是关于两个dataSet 分别从不同的数据源取数据,然后进行merge 后,更新其中一个数据源
cn.Open();
                SqlCommand cmd 
=   new  SqlCommand(sqlAllCustomers, cn);
                SqlDataAdapter adpt 
=   new  SqlDataAdapter(cmd);
               

                
//  Add
                DataSet dsCust1  =   new  DataSet();
                DataSet dsCust2 
=   new  DataSet();
                DataSet dsTest 
=   new  DataSet();
                
// adpt.AcceptChangesDuringFill = false;
                
                adpt.Fill(dsCust1, 
" TABLE1 " );
                
// adpt.AcceptChangesDuringFill = true;
                 using  (SqlConnection cnTemp  =   new  SqlConnection(cnStrTemp))
                
dot.gif {
                    
//SqlConnection cnTemp = new SqlConnection(cnStrTemp);
                    cnTemp.Open();
                    SqlCommand cmdTemp 
= new SqlCommand(sqlAllCustomers, cnTemp);
                    SqlDataAdapter adptTemp 
= new SqlDataAdapter(cmdTemp);
                    adptTemp.AcceptChangesDuringFill 
= false;
                    adptTemp.Fill(dsCust2, 
"TABLE1");
                }


                
// adpt.Update(dsCust1, "TABLE1");
                dsCust1.Merge(dsCust2);
                
                
// 111
                SqlCommandBuilder objBuilder  =   new  SqlCommandBuilder(adpt);
                
                
                adpt.UpdateCommand 
=  objBuilder.GetUpdateCommand();
                adpt.InsertCommand 
=  objBuilder.GetInsertCommand();
                adpt.DeleteCommand 
=  objBuilder.GetDeleteCommand();

                DataTable updTable 
=  dsCust1.Tables[ 0 ];
                adpt.Update(updTable.Select(
null , null , DataViewRowState.Added));

其中主要是,对被合并的数据DataAdapter 属性设置为不稳定,更新的时候将取的数据改为add状态才能更新。否则DataSet 虽然merge 了其它的DataSet 表面上虽然数据已经改变了,但是其状态仍然为稳定状态,更新将失败。 

转载于:https://www.cnblogs.com/playboy2005/archive/2006/06/13/424401.html

你可能感兴趣的:(关于DataSet.merge 问题的答案。)