日期:2014-05-18  浏览次数:21111 次

怎么判断Winform程序是否实时连接数据库(网络连接),断网时程序暂停执行,连上后手动操作
怎么判断Winform程序是否实时连接数据库(网络连接),断网时程序暂停执行,连上后手动操作

因为客户需要有无线网络死角的时候,操作时,程序不死掉(可以对前台TRY_CATCH..弹出MSG,然后RETURN,但修改程序量太大)

请教是否有其它方法.
谢谢


------解决方案--------------------
同意楼上的,做个定时服务,总在检测Connection状态,如果失败进行处理程序。
------解决方案--------------------
同意
------解决方案--------------------
给你个代码。。是判断网络链接状态的。(调用win系统内部cmd.exe来判断,你可以吧网址给改了)用个Timer组件定时判断

 //判断网络是否通畅
private void CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
//用true试试
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;

p.Start();
p.StandardInput.WriteLine("ping -n 1 " + strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1)
{
tbTitle.BackColor = Color.GreenYellow;
tbTitle.Refresh();
}
else if (strRst.IndexOf("Destination host unreachable") != -1)
{
tbTitle.BackColor = Color.Red;
tbTitle.Refresh();
}
else if (strRst.IndexOf("Request timed out") != -1)
{
tbTitle.BackColor = Color.Red;
tbTitle.Refresh();
}
else if (strRst.IndexOf("Unknown host") != -1)
{
tbTitle.BackColor = Color.Red;
tbTitle.Refresh();
}
else
{
tbTitle.BackColor = Color.Red;
tbTitle.Refresh();
}
p.Close();
}

 //网路情况
private void button15_Click(object sender, EventArgs e)
{
CmdPing("www.meinvtu8.com");
}
------解决方案--------------------
定时判断网络是否连接。没有连接提示用户
using System.Runtime.InteropServices;
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private static bool IsConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
static void Main(string[] args)
{
Console.WriteLine(IsConnected().ToString());
}
或用ping