繁体   English   中英

复制数据库离线模式

[英]Copy database offline mode

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

对于我当前的项目,它变慢了。 我必须切换到脱机模式,在该模式下数据库被分离并连接。

从C#开始这种过程的最麻烦的方法是什么? 我知道这里有SSIS,但如果可能的话,我不想使用它。 在我的计算机上安装SSIS有点痛苦。

只需从c#发出SQL Server分离和附加命令

像这样

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();

我在我的项目中完成这项工作。 您可以在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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM