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.
I need to find two tables columns and datatypes are same or not.
Quickest way I can think of would be to run 2 select queries and fill DataTables then compare the columns between the 2 tables.
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().GetSch emaTable() ;
con.Close();
con.Open();
SqlCommand cmd1 = new SqlCommand("select top(1) * from table2", con);
DataTable table1 = cmd.ExecuteReader().GetSch emaTable() ;
con.Open();
SqlCommand cmd = new SqlCommand("select top(1) * from table1", con);
DataTable table = cmd.ExecuteReader().GetSch
con.Close();
con.Open();
SqlCommand cmd1 = new SqlCommand("select top(1) * from table2", con);
DataTable table1 = cmd.ExecuteReader().GetSch
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.