怎么判断Winform程序是否实时连接数据库(网络连接),断网时程序暂停执行,连上后手动操作 怎么判断Winform程序是否实时连接数据库(网络连接),断网时程序暂停执行,连上后手动操作因为客户需要有无线网络死角的时候,操作时,程序不死掉(可以对前台TRY_CATCH..弹出MSG,然后RETURN,但修改程序量太大)请教是否有其它方法.谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 同意楼上的,做个定时服务,总在检测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 public String IP; [System.Runtime.InteropServices.DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); public static bool IsConnectedByLAN() { int Desc = 0; return InternetGetConnectedState(out Desc, 0); } public static bool isConnectToInternet() { Ping ping = new Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = "abcdefghijklmnopqrstuvwxy123456"; byte[] buff = Encoding.ASCII.GetBytes(data); PingReply pingReply = ping.Send("www.baidu.com", 200, buff, options); if (pingReply.Status == IPStatus.Success) { return true; } else { return false; } } public static bool isConnectToServer(string ip) { Ping ping = new Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = "abcdefghijklmnopqrstuvwxy123456"; byte[] buff = Encoding.ASCII.GetBytes(data); PingReply pingReply = ping.Send(ip, 200, buff, options); if (pingReply.Status == IPStatus.Success) { return true; } else { return false; } } public static string getIP() { try { string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); System.Net.WebRequest wr = System.Net.WebRequest.Create(uri); System.IO.Stream s = wr.GetResponse().GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default); string all = sr.ReadToEnd(); //读取网站的数据 int i = all.IndexOf("[") + 1; string tempip = all.Substring(i, 15); string ip = tempip.Replace("]", "").Replace(" ", "");//找出i return ip; } catch (Exception ex) { return "没有找到IP"; } } 获取类型长度问题 请教一个动态水晶报表的问题 企业库的data事务和linq to sql的事务无法通用....这个遇到过吗? 像这样的两个函数,仅仅是一个函数参数的不同,就要写成两个函数,请高手赐教了,如何写成一个函数来代替。请看代码: c#中两个窗体之间的数据的传递? main参数问题 请问c#浮点数怎么去控制格式啊 谁帮帮我 最好给个例子 小菜鸟的一个问题~ C#里怎样调用VB写的DLL啊? 初学C#,请教递归问题。 VS2010 水晶报表Ver:13打印预览与打印的纸张方向不同 ds.Tables[0].Rows.Count 能查到0表 但count=0 sql语句没问题,求解释
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
[System.Runtime.InteropServices.DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); public static bool IsConnectedByLAN()
{
int Desc = 0;
return InternetGetConnectedState(out Desc, 0);
}
public static bool isConnectToInternet()
{
Ping ping = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "abcdefghijklmnopqrstuvwxy123456";
byte[] buff = Encoding.ASCII.GetBytes(data);
PingReply pingReply = ping.Send("www.baidu.com", 200, buff, options);
if (pingReply.Status == IPStatus.Success)
{
return true;
}
else { return false; }
}
public static bool isConnectToServer(string ip)
{
Ping ping = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "abcdefghijklmnopqrstuvwxy123456";
byte[] buff = Encoding.ASCII.GetBytes(data);
PingReply pingReply = ping.Send(ip, 200, buff, options);
if (pingReply.Status == IPStatus.Success)
{
return true;
}
else { return false; }
}
public static string getIP()
{
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据 int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i
return ip; }
catch (Exception ex)
{
return "没有找到IP";
}
}