复制数据库脱机模式

时间:2010-02-18 23:30:23

标签: c# sql-server sql-server-2008

我一直在使用SMO来传输数据库。使用TransferDatabase任务从c#处理非常容易。

对于我目前的项目,这会变慢。我必须切换到脱机模式,数据库是分离和分离的。

从c#开始这样一个过程最麻烦的方法是什么?我知道有SSIS,但如果可能,我不想使用它。在我的机器上安装SSIS有点痛苦。

2 个答案:

答案 0 :(得分:5)

只需从c#

发出SQL Server Detach and Attach命令

像这样......

var sourceConnectionString = "Data Source=sourceServer;Initial Catalog=MyDB;Integrated Security=True;";
var destinationConnectionString = "Data Source=destinationServer;Initial Catalog=MyDB;Integrated Security=True;";

var sourceLocalPath = @"C:\MSSQL\DATA\MyDB.mdf";
var destinationLocalPath = @"C:\MSSQL\DATA\MyDB.mdf";

var sourceRemotePath = @"\\ServerNameA\ShareName\MyDB.mdf";
var destinationRemotePath = @"\\ServerNameB\ShareName\MyDB.mdf";

// Make connections
var sourceConnection = new SqlConnection(sourceConnectionString);
sourceConnection.Open();
var destinationConnection = new SqlConnection(destinationConnectionString);
destinationConnection.Open();

// Detach source database
var sourceCommand = new SqlCommand("sp_detach_db MyDB", sourceConnection);
sourceCommand.ExecuteNonQuery();

// Detach destination database
var destinationCommand = new SqlCommand("sp_detach_db MyDB", destinationConnection);
destinationCommand.ExecuteNonQuery();

// Copy database file
File.Copy(sourceRemotePath, destinationRemotePath);

// Re-attach source database
sourceCommand = new SqlCommand("CREATE DATABASE MyDbName ON (FILENAME = '" + sourceLocalPath + "') FOR ATTACH", sourceConnection);
sourceCommand.ExecuteNonQuery();

// Re-attach destination database
destinationCommand = new SqlCommand("CREATE DATABASE MyDbName ON (FILENAME = '" + destinationLocalPath + "') FOR ATTACH", destinationConnection);
destinationCommand.ExecuteNonQuery();

答案 1 :(得分:1)

我在我的项目中做这项工作。 你可以在C#中使用SqlBulkCopy类。这个类非常强大,您可以以最佳性能复制所有数据。 这个类创建一个bcp命令并在你的服务器上运行它。这个命令是这样的:

bcp pubs.dbo.authors out c: emppubauthors.bcp –n –Sstevenw –Usa –P
bcp pubs2.dbo.authors in c: emppubauthors.bcp –n –Sstevenw –Usa –P

此命令有许多开关。请看这段代码:

// Create source connection
SqlConnection source = new SqlConnection(connectionString);
// Create destination connection
SqlConnection destination = new SqlConnection(connectionString);

// Clean up destination table. Your destination database must have the
// table with schema which you are copying data to.
// Before executing this code, you must create a table BulkDataTable
// in your database where you are trying to copy data to.

SqlCommand cmd = new SqlCommand("DELETE FROM BulkDataTable", destination);
// Open source and destination connections.
source.Open();
destination.Open();
cmd.ExecuteNonQuery();
// Select data from Products table
cmd = new SqlCommand("SELECT * FROM Products", source);
// Execute reader
SqlDataReader reader = cmd.ExecuteReader();
// Create SqlBulkCopy
SqlBulkCopy bulkData = new SqlBulkCopy(destination);
// Set destination table name
bulkData.DestinationTableName = "BulkDataTable";
// Write data
bulkData.WriteToServer(reader);
// Close objects
bulkData.Close();
destination.Close();
source.Close();