简体   繁体   English

如何使用.Merge()方法覆盖合并DataTables中的值?

[英]How to override values in merging DataTables using .Merge() method?

Is there a way to override values in merging DataTables using .Merge() method? 有没有一种方法可以覆盖使用.Merge()方法合并DataTable中的值?

For example if I have two DataTables with the same schematics. 例如,如果我有两个具有相同原理图的数据表。

Both have two Columns: Names | Enabled; 两者都有两列: Names | Enabled; Names | Enabled;

DataTable1 first row entries are: Peter Punani | true; DataTable1的第一行条目是: Peter Punani | true; Peter Punani | true;

DataTable2 first row entries are: Peter Punani | false; DataTable2的第一行条目是: Peter Punani | false; Peter Punani | false;

If I merge them using .Merge() method - DataTable1.merge(DataTable2); 如果我使用.Merge()方法合并它们-DataTable1.merge DataTable1.merge(DataTable2); -, DataTable1 will then have 2 rows and look like this: -,DataTable1将具有2行,如下所示:

Names | Enabled;  
Peter Punani | true;  
Peter Punani | false;  

Now obviously if I try to .Update() that using SqlCommandBuilder to my SQL DB it will give me an error statement, because of two identical values in my Primary Key(Names). 现在很明显,如果我尝试使用SqlCommandBuilder到我的SQL DB进行.Update()操作,由于我的主键(名称)中有两个相同的值,它将给我一个错误声明。

But I want DB1 to accept the differences from DB2, so it would look like: (and do that with all other entries that differ from DB1.) 但是我希望DB1接受与DB2的区别,所以它看起来像:(并使用与DB1不同的所有其他条目来做到这一点。)

Names | Enabled;  
Peter Punani | false; 

So what is the best way to merge these tables, so I can update them properly? 那么合并这些表的最佳方法是什么,以便我可以正确地更新它们?

I just implemented my method this way now: 我现在以这种方式实现了我的方法:

public static bool synchSqlData(DataTable UpdateTable, string selectedTableName, SqlConnection sqlCon, DataTable MergeTable, bool merge)
    {
        bool success = false;
        //Mitgabe eines SELECT cmds für SqlCommandBuilder zum generieren von INSERT,DELETE und UPDATE
        using (SqlCommand sqlCom = new SqlCommand("SELECT * FROM dbo." + selectedTableName, sqlCon))
        {
            SqlDataAdapter da = new SqlDataAdapter(sqlCom);
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);

            da.InsertCommand = cmb.GetInsertCommand();
            da.DeleteCommand = cmb.GetDeleteCommand();
            da.UpdateCommand = cmb.GetUpdateCommand();

            //Zuweisen von PKs
            UpdateTable.PrimaryKey = new DataColumn[] { UpdateTable.Columns[0] };
            MergeTable.PrimaryKey = new DataColumn[] { MergeTable.Columns[0] };

            //Zusammenführen der beiden DataTables in UpdateTable
            if (merge)
            {
                //Setzen der Rows auf changed damit sie bei da.Update() auch erkannt werden ;)
                foreach (DataRow dr in MergeTable.Rows)
                {
                    if (dr.RowState == DataRowState.Unchanged)
                    {
                        dr.SetAdded();
                    }
                    //Entfernen von doppelten Zeilen zur Änderung einzelner values
                    DataRow doubleRow = UpdateTable.Rows.Find(dr[0].ToString());
                    if (doubleRow != null)
                    {
                        doubleRow.Delete();
                    }
                }
                UpdateTable.Merge(MergeTable);
                MessageBox.Show("Merge abgeschlossen.");
            }

            try
            {
                da.Update(UpdateTable);
                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error has occured!" + ex.Message);
                success = false;
            }
        }
        return success;
    }

Basically I just delete all duplicate rows before i merge and update, works fine for me but probably not the ideal solution. 基本上,我只是在合并和更新之前删除所有重复的行,对我来说效果很好,但可能不是理想的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM