Sqldependency provides the function of notifying foreground programs of changes to the monitored database.
The specific usage is as follows:
Public form1 ()
{
Initializecomponent ();
Sqldependency. Start (constr );
Newmethod ();
}
String constr = "Data Source =.; initial catalog = assetsmanagedb; uid = sa; Pwd = ****";
Private void newmethod ()
{
Using (sqlconnection con = new sqlconnection (constr ))
{
Con. open ();
Sqlcommand cmd = new sqlcommand ("select * from TT where flag = 1", con );
Sqldependency dependy = new sqldependency (CMD );
Dependy. onchange + = new onchangeeventhandler (dependy_onchange );
Sqldatareader DR = cmd. executereader ();
If (dr. Read ())
{
MessageBox. Show (Dr ["message"]. tostring ());
}
Dr. Close ();
}
}
Void dependy_onchange (Object sender, sqlnotifeventeventargs E)
{
Newmethod ();
}
Provider = Microsoft. Jet. oledb.4.0; Jet oledb: Database Password = 123; user id = admin; Data Source = 123.mdb