I am trying to develop UI in C# .NET to synchronize 7 instances of backup databases with the central database one by one (All holding same schema) .The backup database( all 7 instances client databases) which is brought to the central server in a removable device such pendrive will consist of mdf and ldf files from each client and will be attached to the server where the central database resides. After all the client backup databases are attached i need to synchronize(update existing data or insert new data to the central database residing in server) each backup database one by one to central database. I want to know as how i can synchronize betweeen a backup database with a central database using C# .NET
Assuming that you will not be altering data from the other 6 databases via each instance you could keep some kind of log of record changes, read that and then update your central database based on the information stored in your log for each database.
If you also need to be updating each of the other 6 instances from each loaded instance, then you'll need to perform an aggregation of the overall changes then send them to each other.
you could also use row versioning to do compares of data, and load it to your central one via that (but you'll need to read rows of each table to find this).
If you also need to update data from your central database -> your client ones you could use a similar technique.
Edit
To perform the actual Update you will need to have a DataReader open to your "Source" database that finds the records.
Then from this object create a secondary reader that
"select **modified ** columns from each of the designated tables" and read that
You then create a command object and ExecuteNonQuery against your Destination database.
Related
I'm trying to use sync framework to synchronize large databases,but since the sizes of databases, it is really painful to deprovision and reprovision,when there are schema changes. Since the project is in Development stage, I want a fixed solution to provision client database without any time wasting way.
My question is, is it possible to restore a provisioned server DB to client DB & run PerformPostRestoreFixup in client DB to save initial sync time?(also vice versa)
yes, that's your only other alternative for initialising new replicas with pre-loaded data. (the other one is generating snapshots via SQL CE).
I have MS SQL Server 2008 and a program written in C#, using Entity Framework. Without using any third-party tools, can I:
Do daily backup to a file of only certain tables from the database?
Programmatically tell the SQL Server "here is the file with some tables, restore it to the new database A" and then connect to the database A and use the data, if needed, delete the database and load next file?
You could. The daily backup should be done by SQL then you program would be used to locate the backup and restore it to a NEW DB. You can do the backup outside of SQL but not sure why you would need/want to.
Steps would be
- create new empty DB
- restore backup to new empty DB
- fix user permissions on DB (if applicable)
You can overload the constructor of you EF model to take in a connection string that you would make on the fly based on the DB you just reloaded.
As far as ony backing up and restoring some tables you may want to try the following.
Create seperate DB for the "data heavy" items that you do not need to backup and restore as often or that have no value for this process.
Move these items from the current DB to the New DB
Add views in the original DB that have the same exact name as the tables that moved out and pull in a views of the new tables on the second DB
As long as the views are names 100% the same name as the tables that you moved and the view is ONLY on the single table your program should not know the difference but you would now be able to backup the smaller DB and not include all the file and image data. Hope this makes sense
Can try to provide more info if you have a more pointed question
Could we create a WCF Service to merge sqlserverce databases in background thread. I have GUI which selects a set of sdf databases from various directories in a network and event handler to cmd for merge the databases in those directories between the range of dates. The structure of the sdf databases are same in all the directories.
Okay...I was trying to be funny with my comment but I will provide a better answer.
I don't know of any "magic" command to merge databases together with C# code. You could write your own logic to merge databases. The code would look something like this.
Get a list of tables from the database.
Iterate through the list of tables and grab the metadata
Add logic to skip any readonly columns such as identity columns and calculated fields
Get the data from the source table
Write the data to the destination table
I would really need much more information to help any more then this. For example,
Will you be merging all the databases into one of the original databases or will you be creating a completely new database which contains all the data from all databases?
Do you need to create the destination database first or does it already exists?
If it already exists will you need to clean up the data that already exists in the database?
Once you write the logic to do the merge, how you call the code is your choice. .Net provides the capability to call the logic from a Web Service and it provides the means to run the code in a background thread. You can implement this functionality in a variety of ways.
This is exactly what SQL Server Integration Services was designed to do. SSIS has source and destination adapters for connecting to SQL CE.
I am using the Microsoft Sync Framework "collaboration" providers. Both ends of the sync will use SQL Express to begin with. When provisioned the database contains a "_tracking" table for each "real" table in the database. My database is fairly large, and I don't want to transfer the entire thing via MSF on the first sync. Is there a way to use some other method to "jumpstart" the sync when both sides are known to contain the same data? In my testing when both databases contain identical content, it looks like it downloads the entire scope, churns through the entire batch of "changes", and then uploads the entire scope back to the server which then churns through the entire dataset again. Is there any way to update the _tracking tables (hopefully only on one side) to let the system know that the database contents are the same?
More information (edit):
From examining the contents of the tracking tables after doing an initial sync, it looks like the scope_update_peer_timestamp and local_create_peer_timestamp fields in every _tracking table need to be updated on both sides. In addition, the update_scope_local_id, scope_update_peer_key, and last_change_datetime need to be set on one of the two sides.
The last_change_datetime field is a datetime and is fairly self-explanatory.
The two _timestamp fields seem to use ##DBTS and are thus bigints that contain the equivalent of an editable timestamp column.
That still leaves a bunch of unknowns:
Does MSF track which peer the content of the timestamp columns come from?
Which peer (local or remote) drives the contents of the _timestamp fields?
What logic drives the contents of the update_scope_local_id and scope_update_peer_key fields?
More information on the environment (edit):
I have SQL Express/Std on both sides. The server side will eventually contain information for several clients (using multi-tenancy), so backups will not be useful since the server will contain information for multiple clients.
how are you initializing your databases? are you provisioning databases that both contain the same set of data already?
the best way to initialize other replicas is to use the GenerateSnapshot method on the SqlCeSyncProvider that creates an SDF file to initialize other replicas or to do a back up of the database (non-SDF, SQL Server/Express database), restore it and run PerformPostRestoreFixup before doing a sync.
I need to update existing data or insert new data from client database say DB1 into central database say DB2 both holding same schema and both databases reside in same machine. The updates are not biderectional. I just want changes to be reflected from client(DB1) to server(DB2).
The client database(DB1) is nothing but the backup database(Full database backup consisting of mdf and ldf files) which is attached to the same server where the central database(DB2) exists. I am not going to make any changes to the backup database(DB1) once it is attached to the server. The backup database(DB1) already has the modified data which i want to update it to central database(DB2) . So how do i do programatically using C# .NET?.Can you give any example code?
I have tried transactional replication with push subscription without sending the snapshot. The problem is that the i want to update the modified data from DB1 to central database DB2 at the first shot itself but transactional replication will not allow me to do so. It will not send any modified data which is already present in DB1. So the initial data present in DB1 is untouched when you try to send without snapshot. The backup database (DB1) already has the modified data prior to replication. How do i tackle this as i am not going to insert any new or modify data into the backup database(DB1) after i set replication.
Thanks and regards,
Pavan
Microsoft Sync framework is the best solution, especially if you are using express editions (in which case replication will not work).
Sync framework is quite straight-forward if used with SQL server change tracking in sql server 2008. You can define your mode of synchronization as well (bi-directional, upload only, download only) and also define what happens when there are conflicts (for instance constraints get violated, etc).
And yeah - just google for an example there are several straight forward walk throughs available on the topic, including peer-peer synchronization (might be the one you require) and client-server synchronization (client should be sql server compact edition).
You may also want to explore SQL Server's merge replication functionality. It is the replication type designed to allow satellite databases to automatically post back their results to a central repository.
To achieve this you have the following options:
1.) Use SQL Server Transactional Replication. Make DB1 as Publisher, DB2 as Subscriber and go for Pull or Push based subscription. All changes in DB1 will be simply reflected to central. If any changes we there in Central for the same tuple, they will be overwritten by DB1 changes.
Advantages: Easy to implement and reliable
Disadvantages: Very little customization
2.) Use Microsoft Sync Framework SQLDataBaseProvider.
Advantages: Very Flexible
Disadvantages: I have heard bad things about it but never tried.
3.) Custom Implementation: This is a bit hard as you need to track changes on DB1. One option can be reading transactional logs which Transactional Replication does internally or other option is to use trigger and build knowledge of changes. Then you need to write a library or routine which will get you change knowledge then it will apply to central.
Edit:
For backup and restore database progmatically:
http://www.mssqltips.com/tip.asp?tip=1849