Skip to content

Latest commit

 

History

History
74 lines (65 loc) · 3.46 KB

File metadata and controls

74 lines (65 loc) · 3.46 KB
title description author ms.author ms.date ms.service ms.topic ms.custom helpviewer_keywords monikerRange
Using Capture Mode
Using Capture Mode
markingmyname
maghan
08/06/2017
sql
reference
ignite-2024
SQL Server Management Objects, capture mode
capture mode [SMO]
SMO [SQL Server], capture mode
=azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

Using Capture Mode

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance Synapse Analytics FabricSQLDB]

SMO programs can capture and record the equivalent [!INCLUDEtsql] statements issued by the program in place of, or in addition to, the statements that are executed by the program. You enable capture mode by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection object, or by using the xref:Microsoft.SqlServer.Management.Smo.Server.ConnectionContext%2A property of the xref:Microsoft.SqlServer.Management.Smo.Server object.

Example

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 Create a Visual C# SMO Project in Visual Studio .NET.

Enabling Capture Mode in Visual Basic

This code example enables capture mode, and then displays the [!INCLUDEtsql] commands held in the capture buffer.

'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

Enabling Capture Mode in Visual C#

This code example enables capture mode, and then displays the [!INCLUDEtsql] commands held in the capture buffer.

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