0

我有一个 VB.Net 程序,它访问 10 个数据库,并为每个数据库填充 10 个不同的数据集。当我们没有任何活动时,该程序会在晚上 11 点自动运行,所以对我来说这不是完成它的最快方式并不重要。

但是,我遇到的问题是:如果我尝试连接到脱机的数据库(互联网中断、VPN 隧道关闭等),程序将无法继续并最终超时。下面是我重复的代码快照每个站点。

Me.1TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.1;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"
Me.2TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.1;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"
Me.3TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.1;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"

然后,我使用上述信息构建一个 HTML 表:

<html>
    <body>
        <table border="1">
            <tr><th>Description</th><th> Location </th></tr>
                <%= From opentime In Me.Dataset.1.AsEnumerable _
                    Select <tr><td>Open Time</td>
                        <td><%= 1.TimeIn.ToString("hh:mm tt") %></td>
                        <td width="50"><%= 1.Name %></td></tr> %>

ETC...

然后我转到下一个数据库:

Me.1TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.2;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"
    Me.2TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.2;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"
    Me.3TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.2;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"

然后我为该数据库构建表:

<html>
    <body>
        <table border="1">
            <tr><th>Description</th><th> Location </th></tr>
            <%= From opentime In Me.Dataset.1.AsEnumerable _
                Select <tr><td>Open Time</td>
                    <td><%= 1.TimeIn.ToString("hh:mm tt") %></td>
                    <td width="50"><%= 1.Name %></td></tr> %>

发生的情况是,如果第一次尝试(数据源 10.0.1.1)超时,或者无法连接程序无法继续,则会出错。如果程序检测到第一个数据源出错,如何让程序转到下一个数据源?请记住,它需要跳过其余的表适配器(在此示例中为 2,对于数据源 10.0.1.1 为 3)。

4

1 回答 1

1

对每个数据库连接使用 Try/Catch 块。

Try
    Me.1TableAdapter.Connection.ConnectionString = "Data Source=10.0.1.1;Initial Catalog=cat;Persist Security Info=True;User ID=id;Password=password;Connection Lifetime=0"
    'your code here to create HTML table'
Catch ex As Exception
End Try
于 2013-05-22T14:54:26.020 回答