【问题标题】:Verify a TableAdapter connection string opens successfully?验证 TableAdapter 连接字符串是否成功打开?
【发布时间】:2011-06-05 12:20:13
【问题描述】:

有什么比 try/catch 包装更好的方法来验证 TableAdapter 上的连接是否打开或将成功打开?

public class MyItemParser
{
     private myTableAdapter fa;

     public FacultyParser()
     {
         this.fa = new facultyTableAdapter();
     }

     public bool HasValidConnection()
     {
        try
        {
             this.fa.Connection.Open();
        }
        catch(exception e)
        {
            return false;
        }     
        return true;
     }

     public void FillList(IList<myItem> list)
     {
         foreach (var row in this.fa.GetData())
             /**** DoSomething  ****/
     }
}

【问题讨论】:

    标签: c# dataset database-connection tableadapter


    【解决方案1】:

    您应该使用 try 和 catch(我通常将连接处理放在不同的类上) 您可能会检查(如果您保持连接打开)连接状态,但如果状态是打开的(不够可靠),这并不意味着任何事情。在一个项目中,我什至发出了一个虚拟 SQL 请求来测试连接,然后再将其处理给使用它的实际类。

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多