how to compare schema and data of two different databases - database

how to compare schema and data of two different databases and synchronize the latest changes in target database through c shrap Code. Note: i'm working on Desktop Application with Sql Server 2014
Explanation:
i'm working with Desktop application on visual studio 2013 and sql server 2014.
i want my application to work with local database, but after a specific time i want to sync new entries OR new schema changes in remote database having the exact same schema as local database. I want solution in c# code. may be stored procedure can also helpful.

Related

Present databases on 2 SQL Servers like they are all on 1

I have 2 SQL Servers, with several different databases on each. I want to present them as a single SQL Server showing all of the databases on either Sql Server.
We have yearly archive databases, so 1 database for each year. This year we decided to deploy a new SQL Server that will host newer years, so older yearly databases are on the old server, and future will be on new one.
I know I can link the servers, but I am not sure if I can do it in the transparent way so that no application has to change the query - so far I always had to use linked server explicitly in each query that uses it.
TLDR: I want to access databases on another SQL Server via main SQL Server so that app doesn't know or care about SQL Server details and don't need to have if (Year > 2021) {then use this server } else {use that server} type of logic and can behave like all databases live on the same server.
You can follow the below steps:
Create a linked server pointing to older server
Create an Empty Database in the new server, corresponding to older database in the older server.
Create synonym for each object in the older database in the Empty database
Now, When you refer to synonyms in the empty database, it will appear as if the older database resides in the new server.

Migrate SQL database from 2014 to 2012 version - mvc codefirst

I'm developing a project with ASP.net-MVC and as it's Model first, it generates the .mdf and .ldf files of database automatically. and database is in version of 2014. when I wanted to upload it on host server they told me that they don't support upper version of 2012. so I need to migrate my data base from 2014 to 2012 version.
my data base is not that much big. and I follow this article
https://www.mssqltips.com/sqlservertip/2810/how-to-migrate-a-sql-server-database-to-a-lower-version/
step by step but no diffrence happend to my database version. its still Product Version:12.0.2000.8
really appreciate you'r help. thanks
You develop the database based on the Model First, so you have all control in generation /modifying the database.
You needn't to migrate the database, but in visual studio you can re-generate the database in sql server 2012.
To Generate the database in sql 2012, do the same steps you follow to generate database for sql 2014 in visual studio, just for reminding:
In the EDMX design surface, right click ->Generate database from the model
Follow the wizard and create a new connection to sql2012.
At end of the wizard, a new database is created in sql server 2012.
Sql script is also auto generated named e.g. model.edmx.sql including all DDL of the Entity and associations,...
Exute that script within VS, you can select the connection and executing the script in the new database server 2012.
For the data, you can use the bcp utility or the export wizard in SSMS.
Update:
In the Model First approach, you create a diagram that will be automatically converted to a coded model and the model is saved in EDMX (xml file).
If there is no EDMX in the project, it means that you didn't use the ModelFirst approach, but you may used a template of MVC project which auto generate database at the start of the project.
The benefit of the Model First is the ease of change and sync between development Environment and the production for future changes.
You can re-engineer your project and add ModelFirst approach even you created a database.
From there you can re-create the database in the sql server 2012.
I describe step by step tutorial to build your mode:
Building DataModel from Existing Database in EntityFramework 6 For ModelFirst Approach
Compatibility Level should be set to SQL Server 2012(110) before to following the steps https://www.mssqltips.com/sqlservertip/2810/how-to-migrate-a-sql-server-database-to-a-lower-version/
To change the compatibility level go to database properties->Options->Compatibility Level
When you run the output script, it should be run on the target server (2012). Backups on recent versions cannot be restored on earlier versions.
Take a look here
The setup will change your database from 2014 to 2012.

Why is SQL Server Object Explorer in Visual Studio so slow?

I just created a new SQL Server Database in Azure and then opened it in Visual Studio 2015 using the link in the Azure Portal. I had to add my IP to the firewall but otherwise the process went smoothly.
However, when I am trying to interact with the database server via SQL Server Object Explorer it is painfully slow. Expanding any of the folders in my Database (e.g., Tables folder) takes 10 to 30 seconds. The database is brand new, so the only things it has are whatever Azure creates when it instantiates a new DB.
This is the second Azure DB I have created and tried to view in Visual Studio and both have the same problem. With the first one I thought maybe I did something wrong during setup but this time I made sure to do everything by the book.
Running actual queries against the DB from within Visual Studio (right click the DB, New Query ..., select * from INFORMATION_SCHEMA.TABLES;) is very fast, so it doesn't appear to be a problem with my connection to Azure.
Why is it so painfully slow? What can I do to make it faster?
I am using Visual Studio 2015 Update 1 (14.0.24720.00) on Windows 10 (fully patched) and during database creation I checked the box to use the latest version.
SQL Server Object Explorer (SSDT) in Visual Studio builds a schema model of entire database when you connect a database first time. This is a concept of model based database development which provides benefits such as validating scripts against database without needing to modify the live database etc. When it comes to Azure SQL DB where the resource is limited with your database's pricing tier, the performance of executing queries to fetch database model can vary. Based on our performance tests, pricing tiers above S1/S2 provides a similar interactivity as local or on-prem SQL instance.
More in details, current SSDT's mechanism is to composite minimum number of queries to minimize the number of round-trip operations with SQL server/database hence each query execution time is longer. It affects the interactivity of SQL Server Object Explorer while the query is being executed.
To enhance, SSDT team is revising the design for Azure SQLDB to provide a faster interaction experience while keeping the benefit of model based database development.
Disclosure: I work on the SQL Data Tools team as a PM
Good tip from #eric-kang in his comment to use 'Server Explorer' instead of 'SQL Server Object Explorer' - this is much better for basic data queries as its actually usable in real time

Sync two or more SQL Server 2012 databases

I am working on SQL Server 2012 and my requirement is to synchronize two or more databases that exist on different servers or locations.
I have tried this with 2008 by using synchronized option appear when we right click on database name.
But did not get any solution for SQL Server 2012.
I have done this successfully using Service Broker on a database that needed to sync only specific data (ie. sync clients and orders between a web and a local database).
It is a bit painful to set up, but works brilliantly once in place.
That said, I wouldn't do it if you need to replicate the entire database.

SQL Server 2000/2005 - Maintaining Development Version and Production Version

I am used to using Oracle Designer by creating database object definitions, then creating DDL SQL required by connecting to a database instance.
For eaxmple, when adding a new column to a table, first I add it to the table definition, then generate the DDL SQL against the development version and run it, then after testing I point to the Production instance to create the missing column in Production.
Given that there is no Designer in SQL Server what is the correct procedure to say add a column to both database instances? Is there a tool for keeping the schemas in sync?
RedGate offer a product called SQL Compare, that can be used to script out the differences between different databases.
See: http://www.red-gate.com/products/SQL_Compare/index.htm
You could also use the Database Diagrams within SQL Server Management Studio to design your database schema however this would be constrained to within a given database.
There isn't a built in tool for keeping them in sync (that I know of), but RedGate has some commercial apps for this.
What do you mean there is no designer in SQL Server? Are you using SQL Server Management Studio?

Resources