我有一个SQL 2008数据库。我正在运行备份的数据库了,然后尝试更新它的形式。如果更新失败的想法是恢复备份。这里是code我使用还原备份。
公共无效RestoreDatabase(字符串数据库名称,字符串backUpFile,字符串的serverName,用户名字符串,字符串密码)
{
恢复sqlRestore =新的恢复();
BackupDeviceItem deviceItem =新BackupDeviceItem(backUpFile,DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database =数据库名;
ServerConnection连接=新ServerConnection(服务器,用户名,密码);
服务器SQLSERVER =新服务器(连接);
sqlRestore.Action = RestoreActionType.Database;
字符串LOGFILE = System.IO.Path.GetDirectoryName(backUpFile);
LOGFILE = System.IO.Path.Combine(日志文件,数据库名+_Log.ldf);
字符串数据文件= System.IO.Path.GetDirectoryName(backUpFile);
数据文件= System.IO.Path.Combine(数据文件,数据库名+.MDF);
数据库DB = sqlServer.Databases [DATABASENAME]
RelocateFile RF =新RelocateFile(数据库名称,数据文件);
sqlRestore.RelocateFiles.Add(新RelocateFile(数据库名称,数据文件));
sqlRestore.RelocateFiles.Add(新RelocateFile(DATABASENAME +_log,日志文件));
sqlRestore.SqlRestore(SQLSERVER);
DB = sqlServer.Databases [DATABASENAME]
db.SetOnline();
sqlServer.Refresh();
}
这个问题似乎是,文件名我选是从在线DB不同。我基本上要更换数据库的服务器与备份上。当我打电话SqlRestore我得到一个异常。
主要的例外说
{恢复失败的服务器 'localhost'的。 }
挖掘内部异常显示这些错误
在执行时出现异常 一个Transact-SQL语句或批处理。
然后
逻辑文件DB不是的一部分 数据库DB。使用RESTORE FILELISTONLY列出逻辑文件 名。 r nRESTORE数据库是 异常终止。
我认为有一些方法来告诉这个只使用替换现有的数据库为是。
我用code此位得到数据库的文件路径有一个目录来转储备份。也许这可以用来获得文件名称来重新创建
公共字符串GetDBFilePath(字符串数据库名称,用户名字符串,字符串密码,字符串服务器名)
{
ServerConnection连接=新ServerConnection(服务器,用户名,密码);
服务器SQLSERVER =新服务器(连接);
数据库DB = sqlServer.Databases [DATABASENAME]
返回sqlServer.Databases [DATABASENAME] .PrimaryFilePath;
}
解决方案
我改变了我的备份和恢复功能,如下所示:
公共无效BackupDatabase(SqlConnectionStringBuilder CSB,字符串的DestinationPath)
{
ServerConnection连接=新ServerConnection(csb.DataSource,csb.UserID,csb.Password);
服务器SQLSERVER =新服务器(连接);
备份bkpDatabase =新的Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Database = csb.InitialCatalog;
BackupDeviceItem bkpDevice =新BackupDeviceItem(的DestinationPath,DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.SqlBackup(SQLSERVER);
connection.Disconnect();
}
公共无效RestoreDatabase(字符串数据库名称,字符串backUpFile,字符串的serverName,用户名字符串,字符串密码)
{
ServerConnection连接=新ServerConnection(服务器,用户名,密码);
服务器SQLSERVER =新服务器(连接);
恢复rstDatabase =新的恢复();
rstDatabase.Action = RestoreActionType.Database;
rstDatabase.Database =数据库名;
BackupDeviceItem bkpDevice =新BackupDeviceItem(backUpFile,DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = TRUE;
rstDatabase.SqlRestore(SQLSERVER);
}
这样,他们只是使用任何文件都在那里。有没有再和指令重新定位文件。
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.rnRESTORE 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;
}
解决方案
I changed my back up and restore functions to look like this:
public void BackupDatabase(SqlConnectionStringBuilder csb, string destinationPath)
{
ServerConnection connection = new ServerConnection(csb.DataSource, csb.UserID, csb.Password);
Server sqlServer = new Server(connection);
Backup bkpDatabase = new Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Database = csb.InitialCatalog;
BackupDeviceItem bkpDevice = new BackupDeviceItem(destinationPath, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.SqlBackup(sqlServer);
connection.Disconnect();
}
public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String userName, String password)
{
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Restore rstDatabase = new Restore();
rstDatabase.Action = RestoreActionType.Database;
rstDatabase.Database = databaseName;
BackupDeviceItem bkpDevice = new BackupDeviceItem(backUpFile, DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = true;
rstDatabase.SqlRestore(sqlServer);
}
That way they just use whatever files are there. There are no longer and directives to relocate files.