views:

183

answers:

3

I'm in a situation where I need to create a partial copy of a database from a remote SQL Server (2005/2008 Express Edition) and store it in a local SQL Server (2005/2008 Express Edition) database. The local copy will have the same schema definition, but contain only parts of the data from the remote database.

The local database has to be given a unique name with parameters from C# code, similar to

TodaysDate_SerialNumber_MachineNumber_DatabaseName    
e.g.  20100622_1_3_DatabaseName

(Don't get caught up with the naming scheme, it hasn't been decided yet.)

I'm working in C# on .net 3.5 using VS2010.

My thoughts so far is to execute the create script for the database on the local SQL server from C# code, and then copy data from the remote database to the local database, filtered on what I actually need. The operations have to be started from C# code, but doesn't necessarily need to be all C# code. But I haven't decided yet. What do you think would be the best option for doing what I want to do?

(Btw, if I'm being unclear, just let me know and I will try to update the question with more info.)

+1  A: 

SqlBulkCopy is probably your best bet if you're doing this entirely in code.

You'll have sql connections to each of your databases, then a select statement to run on your source database which will insert rows into a destination database/table.

PaulG
Without having investigated thoroughly, I like your suggestion the most out of the 3 I have seen so far. I'll do some research and some experiments tomorrow, and then I'll mark the answer that was most useful to me.
Sub-Star
After being sidetracked, I now got around to this. SqlBulkCopy worked well for me. I query the database for all the tables in it, then for each table I query for the columns, and then I generate a Select statement that selects all columns from the table. I then create a SqlDataReader that I give to the SqlBulkCopy object, and voila! I use it to make an exact copy of the data in the database to another database with the same tables and schemas. I use the SqlBulkCopyOptions.KeepIdentity to make sure that my FK relations stay valid. So thank you @PaulG for suggesting the SqlBulkCopy class. =)
Sub-Star
+1  A: 

The answer to this really depends on what you are most comfortable doing.

I'd probably set up a linked server on the local SQL Express and write a single SQL Script to do the whole thing. It could report progress back to the C# app using RAISERROR ... WITH NOWAIT and these messages can be processed asynchronously by setting up a SqlInfoMessageEventHandler

Martin Smith
I'm most comfortable using C#, although I should be able to create a SQL-script as you suggest. I'll do some tests to see what performs best. I have no experience with linked servers though, so I have to look into that. Do you know any good articles that explains linked servers? And an important question: Does linked server work between two SQL Server Express instances?
Sub-Star
@Sub-Star Yes linked servers work on SQL Express. I can see pros and cons of both a C# and a SQL solution. The C# one might be somewhat less efficient as your application is orchestrating each individual step but things like error handling might be easier in C#.
Martin Smith
+1  A: 

Maybe you can create a backup of the database, download it to the local machiene, create there a new, empty database and restore the backup to the empty database with option "override". After that you can delete those records you don't want to "copy".

You could use Microsoft SQL Management Studio to create the backup- and restore-scripts.

Tobi