C# smo还原数据库
我使用SQLServerSMO将.bak恢复到新数据库,但失败 sql server是2012,smo对象版本来自最新的sdk版本11.0 bak文件是使用sql management studio 2012在同一台本地pc上,在同一台编码pc上创建的 我收到的错误消息是: 服务器“服务器”的还原失败 我的代码怎么了C# smo还原数据库,c#,sql-server,sql-server-2012,smo,C#,Sql Server,Sql Server 2012,Smo,我使用SQLServerSMO将.bak恢复到新数据库,但失败 sql server是2012,smo对象版本来自最新的sdk版本11.0 bak文件是使用sql management studio 2012在同一台本地pc上,在同一台编码pc上创建的 我收到的错误消息是: 服务器“服务器”的还原失败 我的代码怎么了 string dbPath = Path.Combine(@"d:\my data", dbName + "_db" + ".mdf"); string logPath = Pat
string dbPath = Path.Combine(@"d:\my data", dbName + "_db" + ".mdf");
string logPath = Path.Combine(@"d:\my data", dbName + "_db" + "_Log.ldf");
Restore restore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem("d:\template.BAK", DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = dbName + "_db";
RelocateFile relocateDataFile = new RelocateFile("Data", dbPath);
RelocateFile relocateLogFile = new RelocateFile("Log", logPath);
restore.RelocateFiles.Add(relocateDataFile);
restore.RelocateFiles.Add(relocateLogFile);
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.SqlRestore(server);
更新:我放弃了SMO解决方案,并尝试了
using (SqlConnection connection = new SqlConnection("Data Source=server;user id=sa;password=xxxxx;"))
{
using (SqlCommand command = new SqlCommand(@"RESTORE DATABASE beauty01 FROM DISK = 'd:\template.bak' WITH RECOVERY, MOVE 'beauty1' TO 'D:\MyData\beauty01_Data.mdf', MOVE 'beauty1_log' TO 'd:\Mydata\beauty01_Log.ldf', REPLACE", connection))
{
connection.Open();
// Add the parameters for the SelectCommand.
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
} >> work good.
谢谢大家。我成功地使用
SMO
恢复了数据库。我将分享我的代码。希望能有帮助。不过,此解决方案有一个警告,它认为您只有一个主数据文件。要匹配日志和数据文件真的很棘手,可能会在很多方面出错。无论如何,试着让我知道这有帮助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.Win32;
namespace DatabaseUtility
{
public class BackupRestore
{
static Server srv;
static ServerConnection conn;
public static void BackupDatabase(string serverName, string databaseName, string filePath)
{
conn = new ServerConnection();
conn.ServerInstance = serverName;
srv = new Server(conn);
try
{
Backup bkp = new Backup();
bkp.Action = BackupActionType.Database;
bkp.Database = databaseName;
bkp.Devices.AddDevice(filePath, DeviceType.File);
bkp.Incremental = false;
bkp.SqlBackup(srv);
conn.Disconnect();
conn = null;
srv = null;
}
catch (SmoException ex)
{
throw new SmoException(ex.Message, ex.InnerException);
}
catch (IOException ex)
{
throw new IOException(ex.Message, ex.InnerException);
}
}
public static void RestoreDatabase(string serverName, string databaseName, string filePath)
{
conn = new ServerConnection();
conn.ServerInstance = serverName;
srv = new Server(conn);
try
{
Restore res = new Restore();
res.Devices.AddDevice(filePath, DeviceType.File);
RelocateFile DataFile = new RelocateFile();
string MDF = res.ReadFileList(srv).Rows[0][1].ToString();
DataFile.LogicalFileName = res.ReadFileList(srv).Rows[0][0].ToString();
DataFile.PhysicalFileName = srv.Databases[databaseName].FileGroups[0].Files[0].FileName;
RelocateFile LogFile = new RelocateFile();
string LDF = res.ReadFileList(srv).Rows[1][1].ToString();
LogFile.LogicalFileName = res.ReadFileList(srv).Rows[1][0].ToString();
LogFile.PhysicalFileName = srv.Databases[databaseName].LogFiles[0].FileName;
res.RelocateFiles.Add(DataFile);
res.RelocateFiles.Add(LogFile);
res.Database = databaseName;
res.NoRecovery = false;
res.ReplaceDatabase = true;
res.SqlRestore(srv);
conn.Disconnect();
}
catch (SmoException ex)
{
throw new SmoException(ex.Message, ex.InnerException);
}
catch (IOException ex)
{
throw new IOException(ex.Message, ex.InnerException);
}
}
public static Server Getdatabases(string serverName)
{
conn = new ServerConnection();
conn.ServerInstance = serverName;
srv = new Server(conn);
conn.Disconnect();
return srv;
}
}
}
是否存在内部异常?请签入调试,这可能会给出真正的原因。另外,您确定没有试图覆盖已存在的文件吗?如果使用相同的
dbName
,则可以使用相同名称的数据和日志文件-请先尝试检查该文件是否存在,如果存在,请不要再次尝试创建该文件。无法打开备份设备“d:\template.BAK”。操作系统错误123(文件名、目录名或卷标语法不正确)。>>.bak是由sql management studio 2012创建的,smo是正确的版本(版本11)。是否存在d:\template.bak
以及您是否有权访问它?D是网络驱动器吗?如果是这样,请尝试将其移动到本地磁盘。删除RelocateFile
文件,它应该可以正常工作;p(如果你不需要的话)这段代码确实帮助了我,只是做了一些修改。我需要将数据库还原到另一个位置,而RelocateFiles
这类东西虽然很复杂,但效果很好。您好,在数据库还原操作中重新定位文件的目的是什么?@RustyWizard有时数据库文件不在默认位置。在这种情况下,我们需要找到该文件并在restore命令中使用它,以便在还原时不会出现错误。永远不会使用MDF和LDF变量!您永远不应该捕捉异常,并按照此处显示的方式重新播放。相反,只需使用“throw;”——这样您就不会丢失堆栈信息