Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two SQL Server database schema in C#

I am releasing a newer version of my Windows application. There are DB schema changes in the new version. Also I do not want to lose the data.

So the approach that I have taken is to replace the dlls while preserving the database. In order to upgarde the database, I am planning to compare the database schema of the old database and make necessary changes.

So how can I compare the database structure (schema) of old one to new one and how can I detect the changes and correct it. What I have tried so far is try and get the database schema using GetSchema method.

But since the new schema is a predefined one, How can I inject the new schema into the program and compare with the existing one in the site where older was installed.

like image 599
Ajay Bhasy Avatar asked Aug 13 '15 12:08

Ajay Bhasy


People also ask

How do I compare schema of two databases in SQL Server?

To compare database definitions. On the Tools menu, select SQL Server, and then click New Schema Comparison. Alternatively, right-click the TradeDev project in Solution Explorer, and select Schema Compare. The Schema Compare window opens, and Visual Studio automatically assigns it a name such as SqlSchemaCompare1 .

How do I compare two database structures?

if you open a model tab, and select the databases menu, you get a compare schemas option, which you can use to compare two different schemas on two different servers, or two schemas on the same server, or a schema and a model, or a lot of other options i haven't tried yet.

How can I compare two SQL files in SQL Server?

Right-click on the right/left panel and click the Difference highlights option from the context menu. Go to the View tab in the main application window and click the Difference highlights button.


2 Answers

We use SQL Compare from RedGate, but it's not particularly cheap.

SQL Compare

This lets us compare the structure of two databases, and creates a SQL script to update one of the databases to match the other.

like image 71
Mike Gledhill Avatar answered Oct 24 '22 10:10

Mike Gledhill


Create your Database Migration scripts and run them with a tool such as Db Up to keep track of schema changes. SQL Scripts migrate your database from version 1 to 2, 2 to 3, etc. Schema Compare is another option mentioned in a previous question.

  • Modify Customer Tables.sql
  • Update Settings.sql
like image 20
lloyd Avatar answered Oct 24 '22 11:10

lloyd