如何将SQL Server数据库备份恢复到新创建的数据库?
我有一个名为 backupdb.bak
的 SQL Server 2005 数据库备份。我想将其恢复到数据库 MyDatabase
。
但在恢复之前,我必须检查 MyDatabase
是否已经存在。如果是这样,我将创建一个名为 MyDatabaseNew
的新数据库,并将备份文件恢复到这个新数据库。
如果我直接将文件恢复到 MyDatabase
中,则该过程运行正常。但是,当我检查 MyDatabase
是否存在并尝试创建名为 MyDatabaseNew
的新数据库时,出现错误:
服务器服务器名恢复失败
我的代码如下所示:
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
CreateDatabase(MyDatabaseNew);
restore.Database = MyDatabaseNew;
restore.ReplaceDatabase = true;
BackupDeviceItem deviceItem = new BackupDeviceItem("C:\\backupdb.bak",DeviceType.File);
restore.Devices.Add(deviceItem);
SqlConnection sqlCon = new SqlConnection("Data Source=.;Initial Catalog=MyDatabaseNewIntegrated Security=True;User ID=uid; Pwd=Pwd");
ServerConnection connection = new ServerConnection(sqlCon);
sqlCon.Open();
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(new ServerConnection("."));
if (File.Exists("C:\\backupdb.bak"))
// restore
restore.SqlRestore(smoServer);
if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
我的创建数据库(如果 MyDatabase
存在)代码如下所示,我将 MyDatabaseNew
作为参数传递这种方法:
public void CreateDatabase(string NewDBName)
{
string str;
SqlConnection myConn = new SqlConnection("Server=.;Integrated security=True;
database=master;User ID=uid;Password=Pwd");
str = "CREATE DATABASE " + NewDBName + "";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("New DataBase is Created Successfully", "Database Creation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Database Creation", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}
}
谁能告诉我哪里出错了?
I've a SQL Server 2005 database backup named backupdb.bak
.I want to restore it to database MyDatabase
.
But before restoring I've to check whether MyDatabase
already exists or not. If it does then I create a new database named MyDatabaseNew
and I restore the backup file to this new database.
If I'm directly restoring the file into MyDatabase
, the process is running fine. But when I'm checking the existence of MyDatabase
and trying to create new database named MyDatabaseNew
, it's giving error:
Restore failed for Server servername
My code looks like this:
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
CreateDatabase(MyDatabaseNew);
restore.Database = MyDatabaseNew;
restore.ReplaceDatabase = true;
BackupDeviceItem deviceItem = new BackupDeviceItem("C:\\backupdb.bak",DeviceType.File);
restore.Devices.Add(deviceItem);
SqlConnection sqlCon = new SqlConnection("Data Source=.;Initial Catalog=MyDatabaseNewIntegrated Security=True;User ID=uid; Pwd=Pwd");
ServerConnection connection = new ServerConnection(sqlCon);
sqlCon.Open();
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(new ServerConnection("."));
if (File.Exists("C:\\backupdb.bak"))
// restore
restore.SqlRestore(smoServer);
if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
and my create database (in case MyDatabase
exists) code looks like this, where I'm passing MyDatabaseNew
as a parameter for this method:
public void CreateDatabase(string NewDBName)
{
string str;
SqlConnection myConn = new SqlConnection("Server=.;Integrated security=True;
database=master;User ID=uid;Password=Pwd");
str = "CREATE DATABASE " + NewDBName + "";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("New DataBase is Created Successfully", "Database Creation",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Database Creation", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}
}
Can any one tell me where I'm going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还原到新数据库时,您需要使用相当于 Transact-SQL 的
RESTORE DATABASE ...WITH MOVE ...
语句 指定数据和日志文件的新名称:更多信息:SQL 2005 中的 SMO 入门 - 恢复
When restoring to the new database, you need to use an equivalent of Transact-SQL's
RESTORE DATABASE ... WITH MOVE ...
statement to specify new names for the data and log files:More information: Getting Started with SMO in SQL 2005 - Restores