Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass second connection string if one failed ? 2

Status
Not open for further replies.

tdong

Programmer
Mar 12, 2004
112
CA
Currently I am doing this Try the first connection if then catch by passing the second one it seems to work but it is effection ? Since we running our server at our site and we have two public ip (2 dsl lines) therefore I think it is more reliable to use both. What do you guys think ? THANKS

Public Sub New()
strConnect = oStat.ConnectionString
Try
oConnection = New SqlConnection(strConnect)
oConnection.Open()
bConnected = True

Catch ex As Exception
oConnection = New SqlConnection(strConnect2)
oConnection.Open()
bConnected = True
'bConnected = False
UpdateEventLog("Connection to SQLServer Failed but Retry ok")
End Try

End Sub
 
I would do it like this....

Code:
Public Sub New()
  strConnect = oStat.ConnectionString
  Try
    oConnection = New SqlConnection(strConnect)
    oConnection.Open()
    
    if Not oConnection.State = Open Then
       oConnection = New SqlConnection(strConnect2)
       oConnection.Open()
       UpdateEventLog("Connection to SQLServer Failed but Retry ok")
    end if

  Catch ex As Exception
    'Check if its the open exception
    If it is the open connection error then
      'Do Nothing
    Else
       Catch it like you normally would
    End If
  End Try
End Sub
 
I try your way but it doesn't work here is what I have and it is working. What do you think ?

Public Sub New()
strConnect = oStat.ConnectionString
bConnected = False
Try
oConnection = New SqlConnection(strConnect)
oConnection.Open()
bConnected = True

Catch ex As Exception
'try another connection since we have two ip at newshore
Try
oConnection = New SqlConnection(strConnect2)
oConnection.Open()
bConnected = True
UpdateEventLog("Connection to SQLServer Failed but Retry ok")
Catch ex2 As Exception
bConnected = False
UpdateEventLog("Connection to SQL Server Failed")
End Try
End Try



End Sub
 
I would split the connections up into their own functions e.g.
Code:
    Sub New()
        If ConnectionOne() = True Then
            ' Continue with connection one
        ElseIf ConnectionTwo() = True Then
            ' Continue with connection two
        Else
            ' Both Failed
        End If
    End Sub

    Private Function ConnectionOne() As Boolean
        Try
            ' Try First Connection
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function ConnectionTwo() As Boolean
        Try
            ' Try Second Connection
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks work great
Public Sub New()

strConnect = oStat.ConnectionString
strConnect2 = oStat.ConnectionString2
If ConnectionOne(strConnect) = True Then
ElseIf ConnectionTwo(strConnect2) = True Then
'UpdateEventLog("Connection to SQLServer Failed but Retry ok")
Else
UpdateEventLog("Connection to SQL Server Failed")
End If

End Sub
Private Function ConnectionOne(ByVal oConnectionString As String) As Boolean
Try
oConnection = New SqlConnection(oConnectionString)
oConnection.Open()
bConnected = True
ConnectionOne = bConnected
Catch ex As Exception
bConnected = False
ConnectionOne = bConnected
End Try
End Function
Private Function ConnectionTwo(ByVal oConnectionString As String) As Boolean
Try
oConnection = New SqlConnection(oConnectionString)
oConnection.Open()
bConnected = True
ConnectionTwo = bConnected
Catch ex As Exception
bConnected = False
ConnectionTwo = bConnected
End Try
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top