Avatar of Sandiya
Sandiya

asked on 

Compare two tables

Hi All,

I want a asp.net ,vb code to compare two datatables and dynamically generate alter table for the missing columns .
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
Sandiya
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

You can use linq. Small example attached.

    Private Sub compareTables()
        Dim Table1 As DataTable = New DataTable("Customers")
        Table1.Columns.Add("Name", Type.GetType("System.String"))
        Table1.Columns.Add("Firstname", Type.GetType("System.String"))
        Table1.Columns.Add("Street", Type.GetType("System.String"))
        Table1.Columns.Add("HouseNumber", Type.GetType("System.String"))
        Table1.Columns.Add("PostalCode", Type.GetType("System.String"))

        Dim Table2 As DataTable = New DataTable("Customers2")
        Table2.Columns.Add("Name", Type.GetType("System.String"))
        Table2.Columns.Add("Firstname", Type.GetType("System.String"))
        Table2.Columns.Add("State", Type.GetType("System.String"))

        Dim firstquery = From col In Table1.Columns Select col.columnName
        Dim notinsecondtable = From col In Table2.Columns Where Not firstquery.Contains(col.columnName)
    End Sub

Open in new window


The last query will tell you that you have a column (namely state) in your second datatable that is not in your first datatable
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sandiya
Sandiya

ASKER

Thanks All
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo