如何从 C# 恢复数据库
我有一个 SQL 2008 数据库。我正在运行一个支持该数据库的表单,然后尝试更新它。如果更新失败,我们的想法是恢复该备份。这是我用来恢复备份的代码。
public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String userName, String password)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
sqlRestore.Action = RestoreActionType.Database;
string logFile = System.IO.Path.GetDirectoryName(backUpFile);
logFile = System.IO.Path.Combine(logFile, databaseName + "_Log.ldf");
string dataFile = System.IO.Path.GetDirectoryName(backUpFile);
dataFile = System.IO.Path.Combine(dataFile, databaseName + ".mdf");
Database db = sqlServer.Databases[databaseName];
RelocateFile rf = new RelocateFile(databaseName, dataFile);
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFile));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFile));
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
问题似乎是我选择的文件名与在线数据库不同。我基本上想用备份替换服务器上的数据库。当我调用 SqlRestore 时出现异常。
主要异常说
{"服务器恢复失败 '本地主机'。 “}
深入研究内部异常会显示这些错误
执行时发生异常 Transact-SQL 语句或批处理。
进而
逻辑文件“DB”不属于 数据库“DB”。使用恢复 FILELISTONLY 列出逻辑文件 名称。\r\n恢复数据库是 异常终止。
我假设有某种方法可以告诉它仅按原样替换现有数据库。
我使用这段代码来获取数据库的文件路径,以便有一个目录来转储备份。也许这可以用来获取要重新创建的文件名。
public string GetDBFilePath(String databaseName, String userName, String password, String serverName)
{
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
return sqlServer.Databases[databaseName].PrimaryFilePath;
}
I have a SQL 2008 DB. I am running a form that backs that DB up, then tries to update it. If the update fails the idea is to restore that backup. Here is the code I am using to restore the backup.
public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String userName, String password)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
sqlRestore.Action = RestoreActionType.Database;
string logFile = System.IO.Path.GetDirectoryName(backUpFile);
logFile = System.IO.Path.Combine(logFile, databaseName + "_Log.ldf");
string dataFile = System.IO.Path.GetDirectoryName(backUpFile);
dataFile = System.IO.Path.Combine(dataFile, databaseName + ".mdf");
Database db = sqlServer.Databases[databaseName];
RelocateFile rf = new RelocateFile(databaseName, dataFile);
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFile));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFile));
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
The issue seems to be that the file names I pick are different from the online DB. I basically want to replace the database on the server with the backup. I get an exception when I call SqlRestore.
The main exception says
{"Restore failed for Server
'localhost'. "}
Digging into the inner exceptions shows these errors
An exception occurred while executing
a Transact-SQL statement or batch.
and then
Logical file 'DB' is not part of
database 'DB'. Use RESTORE
FILELISTONLY to list the logical file
names.\r\nRESTORE DATABASE is
terminating abnormally.
I assume there is some way to tell this to just use replace the existing DB as is.
I use this bit of code to get the file path of the DB to have a directory to dump the backup. Maybe this could be used to get the file names to recreate.
public string GetDBFilePath(String databaseName, String userName, String password, String serverName)
{
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
return sqlServer.Databases[databaseName].PrimaryFilePath;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将备份和恢复功能更改为如下所示:
这样他们就可以使用那里的任何文件。不再有重新定位文件的指令。
I changed my back up and restore functions to look like this:
That way they just use whatever files are there. There are no longer and directives to relocate files.
感谢莱姆斯的回答!
我已将
这两行修改为
并且我的代码运行成功。
感谢您的支持!
Thanks Remus for your answer!
I have modified
these two lines to
and my code is running successfully.
Thanks for the support!
您根据数据库名称添加
RelocateFile
选项,这是不正确的。您应该根据重新定位的每个文件的逻辑文件名添加它们。使用Restore.ReadFileList
检索逻辑文件名列表。
You are adding
RelocateFile
options based on the database name, that is incorrect. You should add them based on the logical file name for each file relocated. UseRestore.ReadFileList
to retrieve the list of logical file names.