【问题标题】:WPF/C# - test connection string and fail elegantlyWPF/C# - 测试连接字符串并优雅地失败
【发布时间】:2009-08-05 14:24:29
【问题描述】:

我有一个使用 LINQ to SQL DataContexts 的 WPF 应用程序。

什么是测试数据库连接并让用户知道连接不良或网络已关闭等的“最佳实践”方式,这样应用程序就不会爆炸。

现在,我看到启动画面,然后在“Windows 正在检查问题的解决方案”时出现“程序已停止工作”。

谢谢!

【问题讨论】:

    标签: c# wpf connection-string


    【解决方案1】:

    尝试在调试模式下运行应用程序。它会告诉你哪个异常被抛出(未捕获的异常会给你你正在谈论的讨厌的spalsh屏幕:))。然后抓住那个异常,然后永远幸福地生活..

    【讨论】:

      【解决方案2】:

      问题是我的数据上下文是通过 XAML 中的 ObjectDataProviders 访问的,因此我没有得到断点。 ObjectDataProvider 是否有某种机制优雅地失败,或者我是否在 MainForm 启动中测试 Connectionstring,在 InitializeComponent 调用之前?

      【讨论】:

      • 不过,在调试模式下,您应该获得有关任何未捕获异常的详细异常信息(除非您在 VS 中将其关闭)
      【解决方案3】:

      尝试(不是双关语)一个 try/catch 语句,您认为它可能会带来一些好处。我正在做的一个应用程序也遇到了类似的问题,这(就其价值而言)是我添加的:

          private void txtCustomerNameSearch_TextChanged(object sender, TextChangedEventArgs e)
          {
              fillCustomerListDataContext dbC = new fillCustomerListDataContext();
      
              var fillCustList = from c in dbC.lstCustomers
                                 where c.CustomerName.StartsWith(txtCustomerNameSearch.Text)
                                 orderby c.CustomerName
                                 select new
                                 {
                                     c.CustomerName,
                                     c.CustomerID
                                 };
      
              try
              {
                  lstCustomerNames.ItemsSource = fillCustList;
                  lstCustomerNames.DisplayMemberPath = "CustomerName";
                  lstCustomerNames.SelectedItem = "CustomerID";
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error = " + ex.Message,"Keeping Amy off balance message");
              }
          }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        • 2017-11-24
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 2017-06-10
        相关资源
        最近更新 更多