SMO 程序可以捕获和记录由程序发出的等效 Transact-SQL 语句,或者除了由程序执行的语句之外。 通过使用对象或使用ConnectionContext对象的属性Server启用捕获模式ServerConnection。
示例:
若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 SQL Server 联机丛书中的“如何:在 Visual Studio .NET 中创建 Visual Basic SMO 项目”或“如何:在 Visual Studio .NET 中创建 Visual C# SMO 项目”。
在 Visual Basic 中启用捕获模式
此代码示例启用捕获模式,然后显示捕获缓冲区中保存的 Transact-SQL 命令。
在 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;
}