Bahasa

Eksekusi SqlCommand dengan SqlNotificationRequest

Untuk menerima pemberitahuan saat hasil kueri berubah, konfigurasikan SqlCommand dengan permintaan pemberitahuan. Gunakan permintaan pemberitahuan untuk antrean server kustom atau saat Anda tidak ingin mempertahankan objek dependensi langsung.

Membuat Permintaan Pemberitahuan

Buat objek SqlNotificationRequest dan tetapkan ke properti perintah Notification sebelum eksekusi. Setelah server mencatat permintaan, Anda tidak perlu mempertahankan objek permintaan. Mengkueri antrean untuk pemberitahuan dan merespons sesuai kebutuhan aplikasi Anda. Pemberitahuan dapat tiba saat aplikasi dimatikan dan tetap tersedia setelah dimulai ulang.

Ketika perintah dengan pemberitahuan terkait dijalankan, setiap perubahan pada kumpulan hasil asli akan memicu pengiriman pesan ke antrean SQL Server yang dikonfigurasi dalam permintaan pemberitahuan.

Cara Anda melakukan polling pada antrean SQL Server dan menginterpretasikan pesan adalah spesifik untuk aplikasi Anda. Aplikasi ini bertanggung jawab untuk melakukan polling antrean dan bereaksi berdasarkan konten pesan.

Nota

Saat menggunakan permintaan pemberitahuan SQL Server dengan SqlDependency, buat nama antrean Anda sendiri alih-alih menggunakan nama layanan default.

Berikan pengguna izin server yang diperlukan untuk meminta pemberitahuan. Kelas SqlNotificationRequest tidak menambahkan persyaratan keamanan sisi klien.

Contoh

Fragmen berikut membuat SqlNotificationRequest objek dan mengaitkannya dengan SqlCommand. Pasang paket NuGet Microsoft.Data.SqlClient dan impor Microsoft.Data.Sql dan Microsoft.Data.SqlClient. Nilai tersebut merujuk pada nama layanan Service Broker dan database yang menghostingnya, bukan antrean itu sendiri. Buat layanan dan antrean seperti yang dijelaskan dalam Mengaktifkan Pemberitahuan Kueri.

' Assume connection is an open SqlConnection.
' Create a new SqlCommand object.
Dim command As New SqlCommand( _
  "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection)

' Create a SqlNotificationRequest object.
Dim notificationRequest As New SqlNotificationRequest()
notificationRequest.UserData = "NotificationID"
notificationRequest.Options = "service=ContactChangeNotifications;local database=AdventureWorks"

' Associate the notification request with the command.
command.Notification = notificationRequest
' Execute the command.
command.ExecuteReader()
' Process the DataReader.
' You can use Transact-SQL syntax to periodically poll the
' SQL Server queue to see if you have a new message.
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
SqlCommand command=new SqlCommand(
 "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection);

// Create a SqlNotificationRequest object.
SqlNotificationRequest notificationRequest=new SqlNotificationRequest();
notificationRequest.UserData = "NotificationID";
notificationRequest.Options = "service=ContactChangeNotifications;local database=AdventureWorks";

// Associate the notification request with the command.
command.Notification=notificationRequest;
// Execute the command.
command.ExecuteReader();
// Process the DataReader.
// You can use Transact-SQL syntax to periodically poll the
// SQL Server queue to see if you have a new message.

Lihat juga