Udostępnij za pośrednictwem


Korzystanie z trybu przechwytywania

Dotyczy:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsBaza danych SQL w usłudze Microsoft Fabric (wersja zapoznawcza)

Programy SMO mogą przechwytywać i rejestrować równoważne instrukcje Transact-SQL wydane przez program zamiast lub oprócz instrukcji wykonywanych przez program. Tryb przechwytywania można włączyć przy użyciu ServerConnection obiektu lub za pomocą ConnectionContext właściwości Server obiektu.

Example

Aby użyć dowolnego podanego przykładu kodu, musisz wybrać środowisko programowania, szablon programowania i język programowania, w którym ma zostać utworzona aplikacja. Aby uzyskać więcej informacji, zobacz Create a Visual C# SMO Project in Visual Studio .NET(Tworzenie projektu SMO w programie Visual Studio .NET).

Włączanie trybu przechwytywania w Visual Basic

Ten przykład kodu umożliwia tryb przechwytywania, a następnie wyświetla polecenia Transact-SQL przechowywane w buforze przechwytywania.

'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

Włączanie trybu przechwytywania w programie Visual C#

Ten przykład kodu umożliwia tryb przechwytywania, a następnie wyświetla polecenia Transact-SQL 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 ( 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;   
}