データベースの変更が通知できるのね
おかわりありませんか? −SqlDependency
今回の作品では、5秒ごとに更新されるデータベースの値に追随する必要がある。
じゃあちょいちょいコマンドを飛ばそうかと思っていたのだが、
有益な情報を得たのでメモメモ。
わんくまBBS
なんでも、SQLServer上で値が更新された事を、
クライアントに通知してくれる機能があるらしい。
(実際には、1プロセスを占有してクライアントが監視し続けるんだそうな)
さっそく試してみよう
msdnのチュートリアルを見つけ、さっそく書いてみることに。
かりかり。
動作させてみた
どれどれ。実行!NotSupportedException はハンドルされませんでした。がくり。
通知機能を使用するにはSQL Server 9.0以降が必要です。
そうか・・・開発環境はSQL Server 2000だった。
本番環境は最新だったはずだけど、テストできないと辛いな・・・
使ったソースコード
msdnから拾ったコードを乗せとく。いつかまた見たくなるかも知れないので。- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.Security.Permissions;
- namespace SqlNotifyingTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- // Following are the declarations used in this program.
- private string statusMessage;
- private DataSet myDataSet = null;
- private SqlConnection connection = null;
- private SqlCommand command = null;
- private string connstr;
- private void button1_Click(object sender, EventArgs e)
- {
- // Remove any existing dependency connection, then create a new one.
- connstr = "Data Source=MANHATTAN;Initial Catalog=SolarMonitorMobile;Persist Security Info=True;User ID=sa;Password=81at84;";
- string ssql = "select * from THATSUDEN_KIROKU ";
- SqlDependency.Stop(connstr);
- SqlDependency.Start(connstr);
- if (connection == null)
- connection = new SqlConnection(connstr);
- if (command == null)
- command = new SqlCommand(ssql, connection);
- if (myDataSet == null)
- myDataSet = new DataSet();
- GetAdvtData();
- }
- private void GetAdvtData()
- {
- myDataSet.Clear();
- // Ensure the command object does not have a notification object.
- command.Notification = null;
- // Create and bind the SqlDependency object to the command object.
- SqlDependency dependency = new SqlDependency(command);
- dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
- using (SqlDataAdapter adapter = new SqlDataAdapter(command))
- {
- adapter.Fill(myDataSet, "THATSUDEN_KIROKU");
- dataGridView1.DataSource = myDataSet;
- dataGridView1.DataMember = "THATSUDEN_KIROKU";
- }
- }
- private bool EnoughPermission()
- {
- SqlClientPermission perm = new SqlClientPermission(PermissionState.Unrestricted);
- try
- {
- perm.Demand();
- return true;
- }
- catch (System.Exception)
- {
- return false;
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // TODO: このコード行はデータを 'solarMonitorMobileDataSet.THATSUDEN_KIROKU' テーブルに読み込みます。必要に応じて移動、または削除をしてください。
- this.tHATSUDEN_KIROKUTableAdapter.Fill(this.solarMonitorMobileDataSet.THATSUDEN_KIROKU);
- }
- delegate void UIDelegate();
- private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
- {
- UIDelegate uidel = new UIDelegate(RefreshData);
- this.Invoke(uidel, null);
- //Remove the handler as it is used for a single notification.
- SqlDependency dependency = (SqlDependency)sender;
- dependency.OnChange -= dependency_OnChange;
- }
- private void RefreshData()
- {
- // Since the code is executing on the UI thread,it is safe to update the UI.
- label1.Text = "Database had some changes and are applied in the Grid";
- // Reload the dataset that is bound to the grid.
- GetAdvtData();
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- SqlDependency.Stop(connstr);
- if (connection != null)
- connection.Close();
- }
- }
- }
Author: Balamurali Balaji MVP