2013-05-28 5 views
3

SQL 스크립트 파일을 실행하는 동안 내 코드가 오류를 무시하도록하려면 어떻게합니까?SQL 스크립트 실행시 오류 무시

SQL 스크립트 파일을 실행하는 동안 오류가 발생하면이 메서드가이를 무시하고 SQL 스크립트 파일을 계속 실행하기를 원합니다. 현재 try-catch 블록을이 메서드 안에 넣으면 오류가 잡히고 예외가 잡히고 오류가 잡히면 SQL 스크립트 파일 실행이 중지됩니다. SQL Server에 오류가 있어도 실행을 계속하고 싶습니다.

어떻게하면됩니까?

내 코드는 다음과 같습니다.

private void RunSQLScript() 
{   
    // file is already defined as a SQL Script file 

    string script = file.OpenText().ReadToEnd(); 

    SqlConnection conn = new SqlConnection(ConnectionString); 

    // split script on GO command 
    IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase); 

    conn.Open(); 

    foreach (string commandString in commandStrings) 
    { 
    if (commandString.Trim() != "") 
     { 
      new SqlCommand(commandString, conn).ExecuteNonQuery(); 
     } 
    } 

    conn.Close(); 

} 

답변

2

try/catch 블록에서 ExecuteNonQuery를 감싸고 catch 블록을 비워 둡니다.

+0

감사합니다. 일했다. 이러한 간단한 작업과 나는 단지 ExecuteNonQuery 부분 대신 전체 메서드에 try-catch를 넣었습니다. – Butters