My application is a WPF C# database app that work with sql Server 2008 and Entity Framework.
我的应用程序是一个WPF C#数据库应用程序,可与sql Server 2008和Entity Framework一起使用。
if sql server Stopped or ..., my application hangs but i want to Show a message to the user if this problem occurred. Please help me how can i do it.
如果sql server停止或...,我的应用程序挂起但我想在发生此问题时向用户显示一条消息。请帮帮我怎样才能做到。
2 个解决方案
#1
2
You can check if sql server service is running
您可以检查sql server服务是否正在运行
http://support.microsoft.com/kb/912426/en-us
and then perform simple select in try-catch block to detect, that you have user rights to database.
然后在try-catch块中执行简单选择以检测您是否拥有数据库的用户权限。
try
{
var b = db.Table.FirstOrDefault();
}
catch(Exception e)
{
ShowMessageBox(e.Message);
}
#2
1
Since - in general - you may have no permissions or ability to check sql server's service status, try to connect to your database with the short timeout (5 sec or less) catch the Exception and show to user what you want.
因为 - 通常 - 您可能没有权限或能力来检查sql server的服务状态,尝试使用短暂超时(5秒或更短)连接到您的数据库捕获异常并向用户显示您想要的内容。
var csb = new SqlConnectionStringBuilder(yourConnectionString);
csb.ConnectTimeout = 5;
try
{
using(var c = new SqlConnection(csb.ToString())
{
c.Open();
}
}
catch(Exception ex)
{
Show the exception to user
}
go on your own
#1
2
You can check if sql server service is running
您可以检查sql server服务是否正在运行
http://support.microsoft.com/kb/912426/en-us
and then perform simple select in try-catch block to detect, that you have user rights to database.
然后在try-catch块中执行简单选择以检测您是否拥有数据库的用户权限。
try
{
var b = db.Table.FirstOrDefault();
}
catch(Exception e)
{
ShowMessageBox(e.Message);
}
#2
1
Since - in general - you may have no permissions or ability to check sql server's service status, try to connect to your database with the short timeout (5 sec or less) catch the Exception and show to user what you want.
因为 - 通常 - 您可能没有权限或能力来检查sql server的服务状态,尝试使用短暂超时(5秒或更短)连接到您的数据库捕获异常并向用户显示您想要的内容。
var csb = new SqlConnectionStringBuilder(yourConnectionString);
csb.ConnectTimeout = 5;
try
{
using(var c = new SqlConnection(csb.ToString())
{
c.Open();
}
}
catch(Exception ex)
{
Show the exception to user
}
go on your own