캡처 모드 사용

SMO 프로그램은 프로그램에서 실행하는 문 대신 실행하거나 문과 함께 실행한 해당 Transact-SQL 문을 캡처하고 기록할 수 있습니다. ServerConnection 개체를 사용하거나 Server 개체의 ConnectionContext 속성을 사용하여 캡처 모드를 설정합니다.

예제

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 SQL Server 온라인 설명서의 "방법: Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기” 또는 “방법: Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기”를 참조하십시오.

Visual Basic에서 캡처 모드 설정

다음 코드 예에서는 캡처 모드를 설정한 다음 캡처 버퍼에 보관된 Transact-SQL 명령을 표시합니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Set the execution mode to CaptureSql for the connection.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql
'Make a modification to the server that is to be captured.
srv.UserOptions.AnsiNulls = True
srv.Alter()
'Iterate through the strings in the capture buffer and display the captured statements.
Dim s As String
For Each s In srv.ConnectionContext.CapturedSql.Text
    Console.WriteLine(s)
Next
'Execute the captured statements.
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text)
'Revert to immediate execution mode. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql

Visual C#에서 캡처 모드 설정

다음 코드 예에서는 캡처 모드를 설정한 다음 캡처 버퍼에 보관된 Transact-SQL 명령을 표시합니다.

{ 
// Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
// Set the execution mode to CaptureSql for the connection. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql; 
// Make a modification to the server that is to be captured. 
srv.UserOptions.AnsiNulls = true; 
srv.Alter(); 
// Iterate through the strings in the capture buffer and display the captured statements. 
string s; 
foreach ( String p_s in srv.ConnectionContext.CapturedSql.Text ) { 
   Console.WriteLine(p_s); 
} 
// Execute the captured statements. 
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text); 
// Revert to immediate execution mode. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql; 
}