English 中文(简体)
如何恢复服务器数据库对新创建的数据库的支持?
原标题:How to restore SQL Server database backup to a newly created database?

I ve a SQL Server 2005 database backup named backupdb.bak.I want to restore it to database MyDatabase.

但是,在恢复I ve之前,检查是否存在<代码>MyDatabase。 如果是的话,我就建立一个名为<代码>的新数据库。 MyDatabase New,我恢复这一新数据库的备份档案。

如果Im将档案直接恢复为MyDatabase,则这一过程将处以罚款。 但是,当我检查了<代码>MyDatabase的存在,并试图创建名为<代码>MyDatabase New的新数据库时,就会发现错误:

Restore failed for Server servername

我的法典认为:

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上)的代码一样。

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();
    }
  }
}

谁能告诉我我我我我我我,我会错吗?

最佳回答
问题回答

暂无回答




相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签