【问题标题】:help with try catch statement帮助 try catch 语句
【发布时间】:2011-06-29 10:25:19
【问题描述】:

我正在创建一个连接到数据库并使用 ODBC 连接器的 web 应用程序,我使用以下代码来执行此操作

private void SetupConnection()
{
    conn.ConnectionString = 
        ConfigurationManager.ConnectionStrings["live"].ConnectionString;

    OdbcDataAdapter da = 
        new OdbcDataAdapter("SELECT * FROM MTD_FIGURE_VIEW1 '", conn);

    da.Fill(ds);
}

我想创建一个 try catch 语句,它将继续尝试连接到数据库。

psudocode会尝试上面的功能,如果连接不上再试一次,如果连接没有任何错误继续进行。

有人可以帮我解决这个问题吗

来自 cmets 的问题相关更新:

我只是希望它继续尝试 原因是 Web 应用程序 我做的永远不会改变它 不断刷新数据,但 数据库从两个小时切换 在此期间每天晚上进行备份 期间我希望应用程序继续运行 尝试连接

【问题讨论】:

  • 我认为在尝试编写任何代码之前,您需要更准确地思考流程。如果连接一直失败,代码应该怎么做?无限循环?还是有最大尝试次数?当达到最大尝试次数时,该怎么办?
  • OdbcDataAdapter 是否真的会在失败时抛出异常?
  • 从用户体验的角度来看,这样不太好,所以会一直连接下去?
  • 我只是希望它继续尝试这样做的原因是我正在制作的 Web 应用程序永远不会切换其不断刷新的数据,但是在此期间数据库每晚会切换两个小时以进行备份期间我希望应用程序在此期间继续尝试连接

标签: c# asp.net try-catch


【解决方案1】:
private void SetupConnection()
{
    conn.ConnectionString = 
        ConfigurationManager.ConnectionStrings["ZenLive"].ConnectionString;

    bool success = false;

    while(!success)
    {
        try
        {
            OdbcDataAdapter da = 
               new OdbcDataAdapter("SELECT * FROM MTD_FIGURE_VIEW1 '", conn);

            da.Fill(ds);
            success = true;
        }
        catch(Exception e)
        {
            Log(e);
            Thread.Sleep(_retryPeriod)
        }
    }
}

请注意,正如人们所评论的那样,这确实不是一个好主意。您需要一些方法来区分数据库已关闭且您想继续重试的“预期”异常,以及发生意外情况且您不想继续尝试的情况。

【讨论】:

    【解决方案2】:

    怎么样,

    const int MaxRetries = 42 //or any other number you like
    
    for (int r = 1; r <= MaxRetries; r++)
    {
        try
        {
            //The thing we want to test
            break;
        }
        catch (SomeExceptionWeWantToIgnore)
        {
            System.Threading.Thread.Sleep(1000) 
            //Or any number of milliseconds you like (not 0)
        }
    } 
    

    如果这是您想做多次并重复使用的事情。 Youi 可以考虑像我在this answer 中所做的那样将它放在一个可重复使用的函数中,警告这可能是矫枉过正。

    【讨论】:

      【解决方案3】:

      如果数据库未启动,则尝试连接它是没有意义的。您只是在浪费时间尝试连接到需要备份的东西。我会做这样的事情:

      DateTime backupStart = Convert.ToDateTime("20:00:00");
      DateTime backupEnd = Convert.ToDateTime("22:00:00");
      DateTime now = DateTime.Now;
      
      while (true)
      {
          // If its backup time then sleep for 2 hours, 0 min and 0 seconds
          if (now > backupStart && now < backupEnd)
          {
              System.Threading.Thread.Sleep(new TimeSpan(2, 0, 0));
              now = DateTime.Now; // So we dont sleep again later
          }
      
          try
          {
              // Try db connect
              // Break if successful
              break;
          }
          catch (Exception ex)
          {
              // Wait 30 seconds then loop then try again
              System.Threading.Thread.Sleep(new TimeSpan(0, 0, 30));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        相关资源
        最近更新 更多