Dela via


Använda avbildningsläge

Gäller för:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL-databas i Förhandsversion av Microsoft Fabric

SMO-program kan samla in och registrera motsvarande Transact-SQL-instruktioner som utfärdats av programmet i stället för, eller utöver, de instruktioner som körs av programmet. Du aktiverar avbildningsläge med hjälp ServerConnection av objektet eller med hjälp ConnectionContext av objektets Server egenskap.

Example

Om du vill använda ett kodexempel som tillhandahålls måste du välja programmeringsmiljö, programmeringsmallen och programmeringsspråket för att skapa ditt program. Mer information finns i Skapa ett Visual C# SMO-projekt i Visual Studio .NET.

Aktivera avbildningsläge i Visual Basic

Det här kodexemplet aktiverar avbildningsläge och visar sedan de Transact-SQL kommandon som finns i avbildningsbufferten.

'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

Aktivera avbildningsläge i Visual C#

Det här kodexemplet aktiverar avbildningsläge och visar sedan de Transact-SQL kommandon som finns i avbildningsbufferten.

{   
// 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;   
}