比较两个数据表并查找哪个列或行已更改

时间:2015-04-14 08:14:38

标签: c# asp.net

下面是我比较两个数据表的代码。

<script runat="server">

    protected void btnCompare_Click(object sender, EventArgs e)
    {
        System.Xml.Schema.XmlSchemaSet schemaSet = new System.Xml.Schema.XmlSchemaSet();

        string fileName12 = System.IO.Path.GetFileName(fuXSD.PostedFile.FileName);
        fuXSD.PostedFile.SaveAs(Server.MapPath("~/" + fileName12));
        string fileBasePath = Server.MapPath("~/");
        string fileName = System.IO.Path.GetFileName(this.fuXSD.FileName);
        string fullFilePath = fileBasePath + fileName;
        schemaSet.Add("http://www.w3.org/2001/XMLSchema", fullFilePath);
        schemaSet.Compile();

        System.Xml.Schema.XmlSchema xmlSchema = null;
        foreach (System.Xml.Schema.XmlSchema schema in schemaSet.Schemas())
        {
            xmlSchema = schema;
        }

        System.Data.DataSet myDS = new System.Data.DataSet();
        myDS.ReadXmlSchema(fuXSD.PostedFile.InputStream);

        System.Data.DataTable dt1 = new System.Data.DataTable();
        dt1.Columns.Add("TableName", Type.GetType("System.String"));
        dt1.Columns.Add("ColumnName", Type.GetType("System.String"));
        dt1.Columns.Add("Datatype", Type.GetType("System.String"));
        dt1.Columns.Add("Length", Type.GetType("System.String"));

        foreach (object item in xmlSchema.Items)
        {
            System.Xml.Schema.XmlSchemaElement schemaElement = item as System.Xml.Schema.XmlSchemaElement;
            System.Xml.Schema.XmlSchemaComplexType complexType = item as System.Xml.Schema.XmlSchemaComplexType;

            if (schemaElement != null)
            {
                System.Xml.Schema.XmlSchemaType schemaType = schemaElement.SchemaType;
                System.Xml.Schema.XmlSchemaComplexType schemaComplexType = schemaType as System.Xml.Schema.XmlSchemaComplexType;

                if (schemaComplexType != null)
                {
                    System.Xml.Schema.XmlSchemaParticle particle = schemaComplexType.Particle;
                    System.Xml.Schema.XmlSchemaSequence sequence = particle as System.Xml.Schema.XmlSchemaSequence;

                    if (sequence != null)
                    {
                        foreach (System.Xml.Schema.XmlSchemaElement childElement in sequence.Items)
                        {
                            if (childElement != null)
                            {
                                System.Xml.Schema.XmlSchemaType schemaType2 = childElement.SchemaType;
                                System.Xml.Schema.XmlSchemaSimpleType schemaSimpleType = schemaType2 as System.Xml.Schema.XmlSchemaSimpleType;
                                System.Xml.Schema.XmlSchemaSimpleTypeRestriction restriction = schemaSimpleType.Content as System.Xml.Schema.XmlSchemaSimpleTypeRestriction;

                                if (restriction != null)
                                {
                                    if (restriction.BaseTypeName.Name != null)
                                    {
                                        foreach (System.Xml.Schema.XmlSchemaObject facet in restriction.Facets)
                                        {
                                            string facetvalue = "";
                                            if (restriction.BaseTypeName.Name.ToLower() == "datetime")
                                            {
                                                facetvalue = "";
                                            }
                                            else
                                            {
                                                facetvalue = ((System.Xml.Schema.XmlSchemaFacet)facet).Value;
                                            }

                                            if (facet is System.Xml.Schema.XmlSchemaMaxLengthFacet || facet is System.Xml.Schema.XmlSchemaMinInclusiveFacet)
                                            {
                                                dt1.Rows.Add(schemaElement.Name, childElement.Name, restriction.BaseTypeName.Name, facetvalue);

                                                //Response.Write(dt);
                                                //Response.Write(schemaElement.Name + " : " + childElement.Name + " : " + restriction.BaseTypeName.Name + " : " + facetvalue + "<br />");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }



        System.Data.DataTable dt2 = new System.Data.DataTable();
        dt2.Columns.Add("TableName", Type.GetType("System.String"));
        dt2.Columns.Add("ColumnName", Type.GetType("System.String"));
        dt2.Columns.Add("Datatype", Type.GetType("System.String"));
        dt2.Columns.Add("Length", Type.GetType("System.String"));

        var reader = new System.IO.StreamReader(fuCSV.PostedFile.InputStream);
        reader.ReadLine();
        while (reader.Peek() != -1)
        {
            var line = reader.ReadLine();
            var values = line.Split(',');

            dt2.Rows.Add(values[0], values[1], values[2], values[3]);

            //Response.Write(dt);
            //Response.Write(values[0] + " " + values[1] + " " + values[2] + " " + values[3] + "<br />");
        }

        ////// Compare Both //////



        dt1.TableName = "FirstTable";
        dt2.TableName = "SecondTable";

        //Create Empty Table
        System.Data.DataTable table = new System.Data.DataTable("Difference");

        try
        {
            //Must use a Dataset to make use of a DataRelation object
            using (System.Data.DataSet ds = new System.Data.DataSet())
            {
                ds.Tables.AddRange(new System.Data.DataTable[] { dt1.Copy(), dt2.Copy() });


                //Get Columns for DataRelation

                System.Data.DataColumn[] firstcolumns = new System.Data.DataColumn[ds.Tables[0].Columns.Count];
                for (int i = 0; i < firstcolumns.Length; i++)
                {
                    firstcolumns[i] = ds.Tables[0].Columns[i];
                }
                System.Data.DataColumn[] secondcolumns = new System.Data.DataColumn[ds.Tables[1].Columns.Count];
                for (int i = 0; i < secondcolumns.Length; i++)
                {
                    secondcolumns[i] = ds.Tables[1].Columns[i];
                }

                //Create DataRelation

                System.Data.DataRelation r = new System.Data.DataRelation(string.Empty, firstcolumns, secondcolumns, false);
                ds.Relations.Add(r);
                for (int i = 0; i < dt1.Columns.Count; i++)
                {
                    table.Columns.Add(dt1.Columns[i].ColumnName, dt1.Columns[i].DataType);
                }
                table.Columns.Add("Message", typeof(string));
                table.BeginLoadData();

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

                    System.Data.DataRow[] childrows = parentrow.GetChildRows(r);
                    if (childrows == null || childrows.Length == 0)
                        table.LoadDataRow(parentrow.ItemArray, true);


                }
                table.EndLoadData();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        Response.Write(table);

        GVDifference.DataSource = table;
        GVDifference.DataBind();
    }

</script>


<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <form runat="server">
        <div>
            <asp:GridView ID="dataGridView1" runat="server"></asp:GridView>
            <asp:FileUpload ID="fuXSD" runat="server" />
            <br />
            <asp:FileUpload ID="fuCSV" runat="server" />
            <br />
            <asp:Button ID="btnCompare" runat="server" OnClick="btnCompare_Click" Text="Compare" />
            <asp:GridView ID="GVDifference" runat="server"></asp:GridView>
        </div>
    </form>
</body>
</html>

以下是我的第一个数据表结果:

TableName       ColumnName      Datatype    Length
CDS_CLHC_TEMP1  CLCL_ID         string       12
CDS_CLHC_TEMP   CLHC_COND_CD1   string        2
CDS_CLHC_TEMP   CLHC_COND_CD2   string        2 
CDS_CLHC_TEMP   CLHC_COND_CD3   string        2

给出另一个数据表结果:

TableName       ColumnName      Datatype    Length
CDS_CLHC_TEMP   CLCL_ID         string       12
CDS_CLHC_TEMP   CLHC_COND_CDPS  string        2
CDS_CLHC_TEMP   CLHC_COND_CD2   Varchar       2 
CDS_CLHC_TEMP   CLHC_COND_CD3   string        2

现在,当我比较两个数据表时,我得到了确切的更改结果,但如下所示:

TableName       ColumnName      Datatype    Length  Message
CDS_CLHC_TEMP1  CLCL_ID         string       12
CDS_CLHC_TEMP   CLHC_COND_CDPS  string        2
CDS_CLHC_TEMP   CLHC_COND_CD2   Varchar       2 

在上表中,我想获取哪些表更改或列更改。这里的第一列CDS_CLHC_TEMP1已更改,因此它应显示表名更改。

0 个答案:

没有答案