Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

How to compare two table schema using c#?

I have table1 and table2 in my sql server  2008 db. How to compare two table schema in c#?
I need to find two tables columns and datatypes are same or not.
Avatar of Russ Suter
Russ Suter

Quickest way I can think of would be to run 2 select queries and fill DataTables then compare the columns between the 2 tables.
Avatar of Varshini S

ASKER

I have used GetSchemaTable method for two table. Now how to compare two datatable schema ?

con.Open();
SqlCommand cmd = new SqlCommand("select top(1) * from  table1", con);
            DataTable table = cmd.ExecuteReader().GetSchemaTable();
            con.Close();
            con.Open();

            SqlCommand cmd1 = new SqlCommand("select top(1) * from  table2", con);
            DataTable table1 = cmd.ExecuteReader().GetSchemaTable();
ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wouldn't spend too much time on this. have a look at http://www.red-gate.com/products/sql-development/sql-compare. it is really worth it
Red Gate products are excellent and well worth the purchase price. But I don't believe it has an in-code solution to this. If he's looking to make a comparison in code he'd need to write something similar to what is provided above.