怎么判断Winform程序是否实时连接数据库(网络连接),断网时程序暂停执行,连上后手动操作因为客户需要有无线网络死角的时候,操作时,程序不死掉(可以对前台TRY_CATCH..弹出MSG,然后RETURN,但修改程序量太大)请教是否有其它方法.
谢谢

解决方案 »

  1.   

    同意楼上的,做个定时服务,总在检测Connection状态,如果失败进行处理程序。
      

  2.   

    给你个代码。。是判断网络链接状态的。(调用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");
            }
      

  3.   

    定时判断网络是否连接。没有连接提示用户
    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
      

  4.   

     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";
                }
            }