远程数据库连接失败或者成功如何快速判断? 每次客户端访问数据库(多点),如果服务器网络通讯中断或者数据库未开启,登录时都非常慢!!会影响客户端界面卡死!用后台线程判断要等很长时间才报异常!网上找了很久也没有好的解决方案!求方法! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } 试过Connection Timeout了吗?"Data Source=...;Initial Catalog=...;Connection Timeout=5"见:http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx 使用连接池没?把TimeOut设短点 TimeOut 好像没有什么反映!而且如果网络中断,那么判断时间就会很长! 可以尝试直接用IP。或用HttpWebRequest.BeginGetResponse,自己进行超时判断。 没有好方法呀!!(1)TimeOut 可以在本地网络中断的时候提速!但是如果远程网络出现问题,就最少15秒延时!(2)PING 可以使用但是有些远程服务器PING不同!寻求方法! 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(); }} 13 楼的var是什么意思.似乎不是C#的 userControl上图形绘制 关于智能画线问题 多客户端数据同步到总服务器 c#怎样用鼠标获取句柄? 如何获取图片的路径############保证够难度############### 求教:WEB中的treeview和框架集的问题 请教C#高手--------------C# 读文件 问题 指针问题 .netcore3.0居然支持COM了 C#应用程序问题 页面错误处理 键盘枚举keys怎么表示control和shift同时按下?
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; }
见:http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx
把TimeOut设短点
TimeOut 好像没有什么反映!而且如果网络中断,那么判断时间就会很长!
或用HttpWebRequest.BeginGetResponse,自己进行超时判断。
(2)PING 可以使用但是有些远程服务器PING不同!寻求方法!
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();
}
}