我在这里遇到了问题。如果你们中的任何一方能够在这方面帮助我,那将是明智的。
由于我是新用户,因此我不允许发布新图片。
Table1有两行,数据类似,如下所示(请尝试将其视为DataView)。
RID | Release_Point_ID | short_desc
-------------------------------------------
1007 | SV006 | Boiler1
1008 | SV006 | Boiler2
Table2有一行相同的数据。
RID | Release_Point_ID | short_desc
-------------------------------------------
1017 | SV006 | Boiler
我的代码片段会在找到表2中的数据后扫描并突破循环。
如果您可以看到两个附加的快照以及此邮件,那么这些是两个不同数据表的DataSet视图。在那些快照中,我突出显示了一行“Process_rid”行,行为“100100164”。对于table2也是如此,但这里我们有两行。
我需要区分这两个数据表。我已经给出了用于理解场景的示例快照,我编写的代码是非常基本的但如果它有两个常见/相似的行将跳过该行。
#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")
{
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")
{
//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;
}