Bagikan melalui


Menggunakan Mode Pengambilan

Berlaku untuk: SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

Program SMO dapat menangkap dan merekam pernyataan Transact-SQL yang setara yang dikeluarkan oleh program sebagai pengganti, atau selain itu, pernyataan yang dijalankan oleh program. Anda mengaktifkan mode pengambilan dengan menggunakan ServerConnection objek , atau dengan menggunakan ConnectionContext properti Server objek .

Contoh

Untuk menggunakan contoh kode apa pun yang disediakan, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.

Mengaktifkan Mode Pengambilan di Visual Basic

Contoh kode ini memungkinkan mode pengambilan, lalu menampilkan perintah Transact-SQL yang disimpan dalam buffer pengambilan.

'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

Mengaktifkan Mode Pengambilan di Visual C#

Contoh kode ini memungkinkan mode pengambilan, lalu menampilkan perintah Transact-SQL yang disimpan dalam buffer pengambilan.

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