2011-07-03 50 views
1

恢复我在C#编写的程序使用SQL 2008SQL Server备份和从C#问题

我要备份的数据库,并恢复它。我的备份代码正常工作,但恢复不起作用,数据库变成单个用户。

收到以下错误:

日志的尾部数据库 “医生”还没有被备份。使用 备份日志与NORECOVERY备份 日志如果它包含工作,你不想 想要失去。使用WITH REPLACE或 WITH RESTORE 语句的STOPAT子句仅覆盖日志的内容 。 RESTORE DATABASE 正在异常终止。 不合格交易正在回滚 。估计回滚

我的代码:

private void Backup(string strFileName) 
{ 
    try 
    { 
     string command = @"BACKUP DATABASE doctor TO DISK='"+ strFileName+"'"; 
     SqlCommand oCommand = null; 
     SqlConnection oConnection = null; 
     oConnection = new SqlConnection("Data Source=.;Initial Catalog=doctor;Integrated Security=True"); 
     if (oConnection.State != ConnectionState.Open) 
     oConnection.Open(); 
     oCommand = new SqlCommand(command, oConnection); 
     oCommand.ExecuteNonQuery(); 


    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

private void Restore(string strFileName) 
{ 
    try 
    { 
     string command = "ALTER DATABASE doctor SET SINGLE_USER with ROLLBACK IMMEDIATE " + "use master " + " RESTORE DATABASE doctor FROM DISK='" + strFileName + "'"; 
     SqlCommand oCommand = null; 
     SqlConnection oConnection = null; 
     oConnection = new SqlConnection("Data Source=.;Initial Catalog=doctor;Integrated Security=True"); 
     if (oConnection.State == ConnectionState.Closed) 
     { 
      oConnection.Open(); 
      oCommand = new SqlCommand(command, oConnection); 
      oCommand.ExecuteNonQuery();    
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

接受更多的答案,请 –

回答

1

我认为,如果你Concat的

“替换为”

与恢复字符串它会工作。