2011-07-03 5 views
1

SQL 2008을 사용하기 위해 C#으로 프로그램을 작성했습니다.C#에서 SQL Server 백업 및 복원

데이터베이스를 백업하고 복원하려고합니다. 백업용 코드가 올바르게 작동하지만 복원이 작동하지 않으며 데이터베이스가 단일 사용자가됩니다.

는 다음과 같은 오류가 발생합니다 :

에 대한 로그의 꼬리 데이터베이스 는 "의사가"백업되지 않았습니다. 백업을 위해 NORECOVERY를 사용하여 을 잃고 싶지 않은 작업이 포함되어 있으면 로그에 을 백업하십시오. 로그의 내용을 덮어 쓰려면 RESTORE 문의 WITH REPLACE 또는 WITH 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 경우 내가 생각하는 당신의 복원 문자열 작동 것와

을 "로 대체합니다."