比较两个表的差异

比较两个表的差异

本文介绍了比较两个表的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ImportLOBEngine
{

public class CompareDataTables
{
  public static DataTable CompareTables()
    {
      DataTable first;
      DataTable second;
      first = new DataTable ("[ProductionTool].[dbo].[Primary_Table]");
      second = new DataTable ("[ProductionTool].[dbo].[LOB_Master]");

      //Create Empty Table
      DataTable table = new DataTable("Difference");
      try
       {
           //Must use a Dataset to make use of a DataRelation object
          using (DataSet ds = new DataSet())
          {
           //Add tables
           ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });
          //Get Columns for DataRelation
         //DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];
           DataColumn[] firstcolumns = new DataColumn[9];
           firstcolumns[0] = ds.Tables[0].Columns["LICENSTAGER"];
           firstcolumns[1] = ds.Tables[0].Columns["EngineNo"];
           firstcolumns[2] = ds.Tables[0].Columns["HullNo"];
           firstcolumns[3] = ds.Tables[0].Columns["EngineType"];
           firstcolumns[4] = ds.Tables[0].Columns["Owner"];
           firstcolumns[5] = ds.Tables[0].Columns["Yard"];
           firstcolumns[6] = ds.Tables[0].Columns["DesignSpec"];
           firstcolumns[7] = ds.Tables[0].Columns["OrderDate"];
           firstcolumns[8] = ds.Tables[0].Columns["DeliveryDate"];

//for (int i = 0; i < firstcolumns.Length; i++)
//{
// firstcolumns[i] = ds.Tables[0].Columns[i];
//}

//DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
DataColumn[] secondcolumns = new DataColumn[9];
secondcolumns[0] = ds.Tables[1].Columns["Licensee"];
secondcolumns[1] = ds.Tables[1].Columns["ENGINENO"];
secondcolumns[2] = ds.Tables[1].Columns["HULLNO"];
secondcolumns[3] = ds.Tables[1].Columns["ENGINETYPE"];
secondcolumns[4] = ds.Tables[1].Columns["OWNER"];
secondcolumns[5] = ds.Tables[1].Columns["YARD"];
secondcolumns[6] = ds.Tables[1].Columns["Spec"];
secondcolumns[7] = ds.Tables[1].Columns["ORDATE"];
secondcolumns[8] = ds.Tables[1].Columns["DLDATE"];
//for (int i = 0; i < secondcolumns.Length; i++)
//{
// secondcolumns[i] = ds.Tables[1].Columns[i];
//}

//Create DataRelation
DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

ds.Relations.Add(r);

//Create columns for return table
for (int i = 0; i < first.Columns.Count; i++)
{
table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
}

//If First Row not in Second, Add to return table.
table.BeginLoadData();

foreach (DataRow parentrow in ds.Tables[0].Rows)
{
DataRow[] childrows = parentrow.GetChildRows(r);
if (childrows == null || childrows.Length == 0)
table.LoadDataRow(parentrow.ItemArray, true);
}

table.EndLoadData();

}
}
catch (Exception ex)
{
throw ex;
}

return table;
}
}
}





给出错误,比如

' column'参数不能为null。参数名称列



Gives error like
'column' argument cannot be null. parameter name column

推荐答案

DataColumn[] firstcolumns = new DataColumn[9];
                    firstcolumns[0] = ds.Tables[0].Columns.Add("LICENSTAGER");
                    firstcolumns[1] = ds.Tables[0].Columns.Add("EngineNo");
                    firstcolumns[2] = ds.Tables[0].Columns.Add("HullNo");
                    firstcolumns[3] = ds.Tables[0].Columns.Add("EngineType");
                    firstcolumns[4] = ds.Tables[0].Columns.Add("Owner");
                    firstcolumns[5] = ds.Tables[0].Columns.Add("Yard");
                    firstcolumns[6] = ds.Tables[0].Columns.Add("DesignSpec");
                    firstcolumns[7] = ds.Tables[0].Columns.Add("OrderDate");
                    firstcolumns[8] = ds.Tables[0].Columns.Add("DeliveryDate");

                    //for (int i = 0; i < firstcolumns.Length; i++)
                    //{
                    // firstcolumns[i] = ds.Tables[0].Columns[i];
                    //}

                    //DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
                    DataColumn[] secondcolumns = new DataColumn[9];
                    secondcolumns[0] = ds.Tables[1].Columns.Add("Licensee");
                    secondcolumns[1] = ds.Tables[1].Columns.Add("ENGINENO");
                    secondcolumns[2] = ds.Tables[1].Columns.Add("HULLNO");
                    secondcolumns[3] = ds.Tables[1].Columns.Add("ENGINETYPE");
                    secondcolumns[4] = ds.Tables[1].Columns.Add("OWNER");
                    secondcolumns[5] = ds.Tables[1].Columns.Add("YARD");
                    secondcolumns[6] = ds.Tables[1].Columns.Add("Spec");
                    secondcolumns[7] = ds.Tables[1].Columns.Add("ORDATE");
                    secondcolumns[8] = ds.Tables[1].Columns.Add("DLDATE");







快乐编码:) :) :)




happy coding :) :) :)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //string str = "\"'" + "Raj" + "\"'";

            //Console.WriteLine(str);
            //Console.ReadLine();

            CompareTables();

        }

        public static DataTable CompareTables()
        {
            DataTable first;
            DataTable second;
            first = new DataTable("[ProductionTool].[dbo].[Primary_Table]");
            second = new DataTable("[ProductionTool].[dbo].[LOB_Master]");

            //Create Empty Table
            DataTable table = new DataTable("Difference");
            try
            {
                //Must use a Dataset to make use of a DataRelation object
                using (DataSet ds = new DataSet())
                {
                    //Add tables
                    ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });
                    //Get Columns for DataRelation
                    //DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];
                    DataColumn[] firstcolumns = new DataColumn[9];
                    firstcolumns[0] = ds.Tables[0].Columns.Add("LICENSTAGER");
                    firstcolumns[1] = ds.Tables[0].Columns.Add("EngineNo");
                    firstcolumns[2] = ds.Tables[0].Columns.Add("HullNo");
                    firstcolumns[3] = ds.Tables[0].Columns.Add("EngineType");
                    firstcolumns[4] = ds.Tables[0].Columns.Add("Owner");
                    firstcolumns[5] = ds.Tables[0].Columns.Add("Yard");
                    firstcolumns[6] = ds.Tables[0].Columns.Add("DesignSpec");
                    firstcolumns[7] = ds.Tables[0].Columns.Add("OrderDate");
                    firstcolumns[8] = ds.Tables[0].Columns.Add("DeliveryDate");


                    //table.Columns.Add(firstcolumns[0], type(System.Int32));

                    for (int i = 0; i < firstcolumns.Length; i++)
                    {

                        first.Columns.Add(firstcolumns[i].ToString());

                    }




                    //table.Columns.Add(firstcolumns);


                    //DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
                    DataColumn[] secondcolumns = new DataColumn[9];
                    secondcolumns[0] = ds.Tables[1].Columns.Add("Licensee");
                    secondcolumns[1] = ds.Tables[1].Columns.Add("ENGINENO");
                    secondcolumns[2] = ds.Tables[1].Columns.Add("HULLNO");
                    secondcolumns[3] = ds.Tables[1].Columns.Add("ENGINETYPE");
                    secondcolumns[4] = ds.Tables[1].Columns.Add("OWNER");
                    secondcolumns[5] = ds.Tables[1].Columns.Add("YARD");
                    secondcolumns[6] = ds.Tables[1].Columns.Add("Spec");
                    secondcolumns[7] = ds.Tables[1].Columns.Add("ORDATE");
                    secondcolumns[8] = ds.Tables[1].Columns.Add("DLDATE");


                    for (int i = 0; i < secondcolumns.Length; i++)
                    {

                        second.Columns.Add(secondcolumns[i].ToString());

                    }
                    //Create DataRelation
                    DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

                    ds.Relations.Add(r);

                    //Create columns for return table
                    for (int i = 0; i < first.Columns.Count; i++)
                    {

                        table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);

                    }



                    //If First Row not in Second, Add to return table.
                    table.BeginLoadData();

                    foreach (DataRow parentrow in ds.Tables[0].Rows)
                    {

                        DataRow[] childrows = parentrow.GetChildRows(r);

                        if (childrows == null || childrows.Length == 0)

                            table.LoadDataRow(parentrow.ItemArray, true);

                    }


                    table.EndLoadData();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return table;
        }
    }
}


for (int i = 0; i &lt; ds.Tables[0].Columns.Count; i++)
                    {
                        table.Columns.Add(ds.Tables[0].Columns[i].ColumnName, ds.Tables[0].Columns[i].DataType);
                    }


//insted of this

for (int i = 0; i &amp;lt; first.Columns.Count; i++)
                   {
                       table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
                   }


这篇关于比较两个表的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 03:41