c# - Track Database Status Change sql server -
i want track database status changed in sql server . in solution ,i have database offline due restore primary server . want hint in application(c#) when database offline , when online. (like sql notification event) . .
one way check state of database using timer application
private void timer1_tick(object sender,eventargs e) { if(checkdatabasestate() == "online") messagebox.show("db online"); else messagebox.show("db not online"); } public string checkdatabasestate() { string constr = @"data source=.\sqlexpress;integrated security=true;initial catalog=master;"; string sql = string.format("select name, state_desc sys.databases name = '{0}'", dbname); sqlconnection conn; sqlcommand comm; sqldataadapter adapter; dataset ds = new dataset(); conn = new sqlconnection(constr); comm = new sqlcommand(sql, conn); adapter = new sqldataadapter(comm); adapter.fill(ds); return ds.tables[0]["state_desc"].tostring(); }
let me know if helps,
Comments
Post a Comment