2012-06-13 3 views
0

C# 스크립트로 여러 데이터베이스를 복원하는 데 어려움을 겪고 있습니다. 또한 오류를 제공하지 않고 단지 아무것도 수행하지 않습니다.코드에서 복원이 작동하지 않고 오류가 발생하지 않습니다.

이 여기에 코드입니다 :

// Connect to SQL Server 
SqlConnection conn = new SqlConnection("Data Source=SERVERNAME;" + "Integrated Security=SSPI;" + "Connection timeout=60"); 

StreamWriter logFile = new StreamWriter(@"F:\Backups\log.txt"); 

try{ 
    conn.Open(); 
}catch(Exception e){ 
    string errorTxt = "There was an error connecting to the server: " + e.ToString(); 
    logFile.WriteLine(errorTxt); 
} 

// Get Directory 
DirectoryInfo source = new DirectoryInfo(@"F:\Backups\SERVERNAME\"); 

foreach(FileInfo fi in source.GetFiles()){ 

    // We need to get the DB name: 
    string filename = fi.Name.ToString(); 
    int bkpIndex = filename.IndexOf("_backup"); 

    string sql = "USE master RESTORE DATABASE " + filename.Substring(0, bkpIndex) + " FROM DISK = '" + fi.FullName + "' WITH REPLACE"; 

    try{ 
     Console.WriteLine("Restoring {0}.", filename.Substring(0, bkpIndex)); 
     logFile.WriteLine("SQL: {0}", sql); 
     SqlCommand cmd = new SqlCommand(sql, conn); 
    }catch(Exception ex){ 
     logFile.WriteLine("Error restoring {0}: " + ex.ToString(), filename.Substring(0, bkpIndex)); 
    } 

} 

logFile.Close() 
conn.Close() 
+0

디버깅 할 때 - 명령을 실행합니까? 로그 파일이 디스크로 비워 지거나 오류가 발생합니까? –

답변

1

외부에서 명령을 실행하지 않는 ... ExecuteNonQuery는보십시오!

SqlCommand cmd = new SqlCommand(sql, conn); 
cmd.ExecuteNonQuery(); 
+0

그게 다예요 ... 그래도 이상하지. 내가 그걸 시도해 보았다고 생각하고 인수를 요청했다. 아마도 다른 Intellisense 태그를 클릭했을 것이다. – user973259

관련 문제