Udostępnij za pośrednictwem


Za pomocą trybu przechwytywania

Obiekty SMO programów można przechwytywać i nagrywać odpowiednik Transact-SQL instrukcje wydawane przez program zamiast lub w połączeniu z instrukcji, które są wykonywane przez program. Włącz tryb przechwytywania przy użyciu ServerConnection obiekt lub przy użyciu ConnectionContext() Właściwość Server obiekt.

Przykład

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see "How to: Create a Visual Basic SMO Project in Visual Studio .NET" or "How to: Create a Visual C# SMO Project in Visual Studio .NET" in SQL Server Books Online.

Włączanie trybu przechwytywania w języku Visual Basic

W tym przykładzie kodu umożliwia przechwytywanie trybu, a następnie wyświetla Transact-SQL polecenia są przechowywane w buforze przechwytywania.

Włączanie trybu przechwytywania w środowisku Visual C#

W tym przykładzie kodu umożliwia przechwytywanie trybu, a następnie wyświetla Transact-SQL polecenia są przechowywane w buforze przechwytywania.

{ 
//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 ( s in srv.ConnectionContext.CapturedSql.Text) { 
   Console.WriteLine(s); 
} 
//Execute the captured statements. 
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text); 
//Revert to immediate execution mode. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql; 
}