验证 TableAdapter 连接字符串是否成功打开?

发布于 2024-10-10 18:31:31 字数 616 浏览 0 评论 0原文

有什么比 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  ****/
     }
}

What is a better way than try/catch wrapping to verify that the connection on a TableAdapter opened or will open successfully?

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  ****/
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-10-17 18:31:31

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

You should use the try and catch (I usually put the connection handling on a different class)
You might check (if you keep the connection open ) for the connection state but it doesn't mean anything if the state is open (not reliable enough). In one project I even issued a dummy SQL request to test the connection before I handle it to the actuall class that uses it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文