跟踪数据库状态更改SQL Server [英] Track Database Status Change sql server
本文介绍了跟踪数据库状态更改SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想跟踪sql server中更改的数据库状态。在我的解决方案中,由于从主服务器还原,我有一个脱机的数据库。
我想在数据库离线和在线时在我的应用程序(C#)中获得提示。 (如sql通知事件)。
谢谢。
I want to Track database status changed in sql server . in my solution ,I have a database that is offline due to restore from primary server . i want to get hint in my application(C#) when database is offline and when is online. (like sql notification event) . Thanks .
推荐答案
一种方法是使用应用程序中的计时器来检查数据库状态
One way is to check the state of the database using a timer from your application
private void timer1_tick(object sender,EventArgs e)
{
if(CheckDatabaseState() == "ONLINE")
MessageBox.Show("db is online");
else
MessageBox.Show("db is NOT online");
}
Public string CheckDatabaseState()
{
string Constr = @"Data Source=.\SQLEXPRESS;Integrated Security=True;Initial Catalog=master;";
string sql = string.Format("SELECT Name, state_desc FROM sys.databases where 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();
}
让我知道是否有帮助,
这篇关于跟踪数据库状态更改SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文