简体   繁体   中英

How to Loop through multiple Database connections skipping on connection error

I have a VB.Net program, that accesses 10 Databases, and fills 10 different datasets per database. The program runs automatically at 11PM when we do not have any activity so it didn't matter to me that it was not the fastest way to accomplish it.

However, The problem I have is: if I try to connect to a database that is offline (internet outage, VPN tunnel down, etc. the program will not continue and eventually times out. Below is a snapshot of my code that is repeated for each site.

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"

I then build an HTML table with the above information:

<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...

I then go to the next database:

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"

And then I build the table for that database:

<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> %>

What Happens is, if the first attempt (Datasource 10.0.1.1) times out, or cannot connect the program doesn't continue, it errors. How can I get the program to go to the next datasource if it detects an error with the first datasource? Keep in mind it needs to skip the remaining table adapters (in this example 2, and 3 for Data Source 10.0.1.1).

Use a Try/Catch block for every connection to database.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM