如何将SQL Server数据库备份恢复到新创建的数据库?

发布于 2024-12-17 08:28:01 字数 2077 浏览 2 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-12-24 08:28:01

还原到新数据库时,您需要使用相当于 Transact-SQL 的 RESTORE DATABASE ...WITH MOVE ... 语句 指定数据和日志文件的新名称:

restore.RelocateFiles.Add(new RelocateFile("MyDatabase", @"c:\MyDatabaseNew.mdf"));
restore.RelocateFiles.Add(new RelocateFile("MyDatabase_Log", @"c:\MyDatabaseNew.ldf"));

更多信息: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:

restore.RelocateFiles.Add(new RelocateFile("MyDatabase", @"c:\MyDatabaseNew.mdf"));
restore.RelocateFiles.Add(new RelocateFile("MyDatabase_Log", @"c:\MyDatabaseNew.ldf"));

More information: Getting Started with SMO in SQL 2005 - Restores

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文