远程数据库连接失败或者成功如何快速判断?

pfworld 2010-11-29 09:34:11
每次客户端访问数据库(多点),如果服务器网络通讯中断或者数据库未开启,登录时都非常慢!!会影响客户端界面卡死!
用后台线程判断要等很长时间才报异常!网上找了很久也没有好的解决方案!求方法!
...全文
348 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanhaiqing 2010-12-31
  • 打赏
  • 举报
回复
13 楼的var是什么意思.似乎不是C#的
pfworld 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jshi123 的回复:]
C# code

public static bool TestConnection(string host, int port, int millisecondsTimeout)
{
var client = new TcpClient();
try
{
var ar = client.BeginConnect(host, port, nul……
[/Quote]


太好了,这个就是我要找的!!谢谢!!
zhaomeng1230 2010-12-01
  • 打赏
  • 举报
回复
这个 确实没有想到的 帮顶个吧
pfworld 2010-12-01
  • 打赏
  • 举报
回复
自己UP一下!!UP有分呀!!
jshi123 2010-12-01
  • 打赏
  • 举报
回复

public static bool TestConnection(string host, int port, int millisecondsTimeout)
{
var client = new TcpClient();
try
{
var ar = client.BeginConnect(host, port, null, null);
ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
return client.Connected;
}
catch
{
return false;
}
finally
{
client.Close();
}
}
bbb332 2010-12-01
  • 打赏
  • 举报
回复
要不设计一个时间范围,超出之后就直接说:网速不好。。。。嘎嘎。。。。
一只熊猫 2010-12-01
  • 打赏
  • 举报
回复
我也没什么好办法 。顶起,看有没有高手来说说。
pfworld 2010-12-01
  • 打赏
  • 举报
回复
没有好方法呀!!

(1)TimeOut 可以在本地网络中断的时候提速!但是如果远程网络出现问题,就最少15秒延时!
(2)PING 可以使用但是有些远程服务器PING不同!

寻求方法!
gomoku 2010-11-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pfworld 的回复:]
...
而且如果网络中断,那么判断时间就会很长!
[/Quote]

[Quote=同样出自2楼的引用:]
...
域名系统 (DNS) 查询可能需要 15 秒返回或超时。如果您的请求包含要求解析的主机名,并且您将 Timeout 设置为小于 15 秒的值,则在 15 秒或更长时间之后才会引发 WebException 以指示您的请求超时...
[/Quote]

可以尝试直接用IP。
或用HttpWebRequest.BeginGetResponse,自己进行超时判断。
pfworld 2010-11-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yuxuanji 的回复:]
使用连接池没?
把TimeOut设短点
[/Quote]

TimeOut 好像没有什么反映!而且如果网络中断,那么判断时间就会很长!
LutzMark 2010-11-29
  • 打赏
  • 举报
回复
使用连接池没?
把TimeOut设短点
gomoku 2010-11-29
  • 打赏
  • 举报
回复
试过Connection Timeout了吗?

"Data Source=...;Initial Catalog=...;Connection Timeout=5"
见:http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx

q107770540 2010-11-29
  • 打赏
  • 举报
回复

public static bool TestConnection(string ConnectionString)
{
bool result = true;
try
{
SqlConnection m_myConnection = new SqlConnection(ConnectionString);
m_myConnection.Open();
m_myConnection.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
result = false;
}
return result;

}
pfworld 2010-11-29
  • 打赏
  • 举报
回复
自己UP一下!!UP有分呀!!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧