使用捕获模式
适用于:SQL Server Azure SQL 数据库 azure Synapse Analytics Azure SQL 托管实例
SMO 程序可以捕获和记录由程序发出的等效 Transact-SQL 语句,或者除了由程序执行的语句之外。 通过使用 ServerConnection 对象,或者通过使用 ConnectionContext 对象的 Server 属性,启用捕获模式。
示例
若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 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;
}