开发者

difference between two datatables

开发者 https://www.devze.com 2023-03-15 17:00 出处:网络
I\'ve a problem here. If any of you can please help me in this regard it would be geat. As I\'m new user it doesn\'t allow me to post new images.

I've a problem here. If any of you can please help me in this regard it would be geat.

As I'm new user it doesn't allow me to post new images.

Table1 has two rows with similar data as shown below (Please try to look it as a DataView).

RID      | Release_Point_ID   | short_desc
-------------------------------------------
1007     | SV006              | Boiler1
1008     | SV006              | Boiler2

Table2 has one row of same data.

RID      | Release_Point_ID   | short_desc
-------------------------------------------
1017     | SV006              | Boiler

My code snippet below scans and breaks out of the loop once it finds the data in Table2.

If you can see two attached snap shots along with this mail, those are the DataSet views for two different datatables. In those snap shots I've highlighted a line for the column Process_rid with row "100100164". And the same this for table2 but here we have two rows.

I need to get the difference between these two datatables. I've given example snap shots for understanding the scenario, the code I've written is very basic one but will skip the row if it has two common/similar rows.

#region get Difference Table
    public static bool isEqual;
    public static DataTable table;
    public static string table2PrvRow, tablePrvRow;
    public static int tblRow = 0, tableRCount = 0;
    // This function fetches the row which is different in both the tables.
    public DataTable getDifferenceTable(DataTable table1, DataTable table2)
    {
        //Create Empty Table
        DataTable table = new DataTable("Difference");
        try
        {
            if (table1.Rows.Count > table2.Rows.Count)
            {
                resultTable = table1.Clone();
                DataRow resultRow = resultTable.NewRow();
                if (tblName == "INV_SOURCES")
                {
                    string sourceId = "27" + string.Format("{0:00000000}", getPreferredID(facId));
                    table = _DB.ReadDataTable(string.Format("SELECT SOURCE_NAME FROM RAPIDS.INV_SOURCES WHERE INVENTORY_RID = {0} AND SOURCE_ID = {1}", glblVars.invRid, sourceId));
                }
                else if (tblName == "INV_PROCESS_RELEASE_POINTS")
                    table = _DB.ReadDataTable(string.Format("SELECT IPRP.PROCESS_RID FROM RAPIDS.INV_PROCESS_RELEASE_POINTS IPRP WHERE IPRP.SOURCE_RID = {0}", glblVars.RapidsSourceRID));
                for (int row = 0; row < table.Rows.Count; row++)
                {
                    foreach (DataRow rpIDRow in table2.Rows)
                    {
                        if (tblName == "INV_SOURCES" && rpIDRow["SOURCE_NAME"].ToString() == table1.Rows[row]["SOURCE_NAME"].ToString())
                        { copy = "N"; break; }
                        else if (tblName == "INV_PROCESS_RELEASE_POINTS" && rpIDRow["PROCESS_RID"].ToString() == table1.Rows[row]["PROCESS_RID"].ToString())
                        { copy = "N"; break; }
                        else
                        { copy = "Y"; }
                    }

                    if (copy == "Y")
                    { resultRow = resultTable.NewRow(); resultRow.ItemArray = table1.Rows[row].ItemArray; resultTable.Rows.Add(resultRow); }
                }
            }
            else if (table1.Rows.Count < table2.Rows.Count)
            {
                resultTable = table2.Clone();
                DataRow resultRow = resultTable.NewRow();
                if (tblName == "INV_SOURCES")
                {
                    st开发者_开发问答ring sourceId = "27" + string.Format("{0:00000000}", getPreferredID(facId));
                    table = _DB.ReadDataTable(string.Format("SELECT SOURCE_NAME FROM RAPIDS.INV_SOURCES WHERE INVENTORY_RID = {0} AND SOURCE_ID = {1}", glblVars.invRid, sourceId));
                }
                else if (tblName == "INV_PROCESS_RELEASE_POINTS")
                {
                    //DataTable differenceOfTwoTables = Difference(table1, table2);
                    table = _DB.ReadDataTable(string.Format("SELECT IPRP.PROCESS_RID FROM RAPIDS.INV_PROCESS_RELEASE_POINTS IPRP WHERE IPRP.SOURCE_RID = {0}", glblVars.RapidsSourceRID));
                }
                for (int row = 0; row < table2.Rows.Count; row++)
                {

                    foreach (DataRow rpIDRow in table.Rows)
                    {
                        tableRCount = 0;
                        if (tblName == "INV_SOURCES")
                        {
                            string table1srcName = rpIDRow["SOURCE_NAME"].ToString();
                            string table2srcName = table2.Rows[row]["SOURCE_NAME"].ToString().Replace("'", "");
                            if (table1srcName == table2srcName)
                            { copy = "N"; break; }
                        }
                        else if (tblName == "INV_PROCESS_RELEASE_POINTS" && rpIDRow["PROCESS_RID"].ToString() == table2.Rows[row]["PROCESS_RID"].ToString())
                        { copy = "N"; break; }

                        else
                        { copy = "Y"; } tableRCount++;
                    }
                    if (copy == "Y")
                    { resultRow = resultTable.NewRow(); resultRow.ItemArray = table2.Rows[row].ItemArray; resultTable.Rows.Add(resultRow); }
                }
            }
            else
                resultTable = table1.Clone();
        }
        catch (Exception ex)
        {
            throw new Exception("Get Difference Table: " + ex.Message);
        }
        return resultTable;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消