我用的是OracleClient做的
但是怎么检测连接已经断开呢,比如网线拔掉或者数据库服务器重启啦。

解决方案 »

  1.   

    public void ShowOracleException() 
    {
       OracleConnection myConnection =
          new OracleConnection("Data Source=Oracle8i;Integrated Security=yes");   try 
       {
          myConnection.Open();
       }
       catch (OracleException e) 
       {
         string errorMessage = "Code: " + e.Code + "\n" +
                               "Message: " + e.Message;     System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
         log.Source = "My Application";
         log.WriteEntry(errorMessage);
         Console.WriteLine("An exception occurred. Please contact your system administrator.");
       }
    }
      

  2.   

    我的意思是说我已经建立好连接,跟楼上的一样。
    已经能正常使用。
    好了,现在中途网络断了。
    我想知道怎么样才能随时检测这个连接是否已经断开。你不运行查询、插入、删除、修改怎么知道网络断开了呢?
    private void Check_L1_Physical_Connection()
    {
    while(ThreadFlag)
    {
    IPAddress myScanIP = IPAddress.Parse("192.168.0.100");
    try
    {
    IPHostEntry iphost = Dns.GetHostByAddress(myScanIP);
    this.img_L1PhysicalConnection.Image = Image.FromFile(Application.StartupPath+"\\"+"connect.bmp");
    }
    catch
    {
    this.img_L1PhysicalConnection.Image = Image.FromFile(Application.StartupPath+"\\"+"disconnect.bmp");
    }
    finally
    {
    Thread.Sleep(20000);
    }
    }
    }此代码可以检测网络通讯是否正常,如果要检测ORACLE是否连接正常,可以把我最上面的代码做成线程,隔一段时间运行一次进行检测。void Check()
    {
       while(true)
       {
          ShowOracleException();
          Thread.Sleep(20000);
       }
    }static void Main()
    {
       new Thread(new ThreadStart(Check)).Start();
    }