问题
I have a local SQL Server Express database that I want to copy to a SQL Server database on a server. I do not have full permissions to the server, so I cannot directly attach it. Somebody told me I could do this by using the SqlBulkCopy[MSDN] class.
I can't figure out how to use it. I haven't done enough database programming to be familiar with how this sort of thing is done.
Basically, I want to copy all the tables in one database to another. There are relationships between the tables, I don't know if that will make a difference in the copying or not. There aren't very many, so it wouldn't be a big deal to just copy the data and then manually reassign the relationships.
This is as far as I could go
var localDatabase = new SqlConnection("connection string");
var serverDatabase = new SqlConnection("other connection string");
SomehowCopyAllTables(localDatabase, serverDatabase);
but unfortunately, Mr. C# didn't want to acknowledge the existence of the SomehowCopyAllTables
method.
How can I copy all the tables from one database to another?
回答1:
Use SQL Server's Generate Scripts command within SQL Server Management Studio.
- right click on the database; Tasks -> Generate Scripts
- select your tables, click Next
- click the Advanced button
- find Types of data to script - make the choice as you like: include data or not.
- you can then choose to save to file, or put in new query window.
- results in
INSERT
statements for all table data selected in bullet 2. - copy/paste that output in the new query window onto your target server.

回答2:
From my point of view, the best way is to create SQL scripts in your SQL Express and execute the generated scripts on your server. The whole data will be copied.
回答3:
To generate database scripts from Sql Server Management Studio:
- Right click the database
- Select Tasks -> Generate Scripts
- (Click next if you get the intro screen)
- Select "Select specific database objects"
- Pick the objects to generate scripts for (tables, stored procedures, etc...)
- Click Next, then specify the output filename
- Click Finish to generate the script
This will generate the schemas only. If you want to do data generating scripts as well, in step 6) click the Advanced button and scroll down to the "Types of data to script" and change it from "Schema only" to "Data only" or "Schema and data"
回答4:
It might be easier to simply use Atlantis Interactive's Schema Inspector and Data Inspector rather than program it yourself. Simply compare the schema to a blank database on your current system.
来源:https://stackoverflow.com/questions/7365071/migrate-sql-server-express-tables-to-sql-server-database