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();
}
}
}
谁能告诉我我我我我我我,我会错吗?