Avatar of jackwebb22002
jackwebb22002
Flag for United States of America

asked on 

Modifying a connection string

I would like to be able to dynamically modify an application's connection string based on a new location of the database.  For instance, I have an application (snippet below) that Gets an SQL Connection string from My.Settings prior to opening it.  Unfortunately, when i deploy, the target computer's database location does not match the copy i have in the development environment.  So basically, I need a way to recover from the connection.open statement timing out.  Ideally, I would propose to do something to update the connection string in the catch block after recording the error message.  

Thanks in advance for any help you may be able to provide.
Friend Class Connection
    Private Shared m_Connection As SqlConnection
    Private Shared m_LastError As String
    'Private connDialog As SqlConnectionStringBuilder
    ''' <summary>
    ''' Shared method that returns a reference to an SqlConnection object for the Travel Database.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>If this is the first time the method is called, a new SqlConnection is constructed.</remarks>
    Public Shared Function GetConnection() As SqlConnection
        If m_Connection Is Nothing Then
            Try
                m_Connection = New SqlConnection(My.Settings.TravelConnectionString)
            Catch ex As Exception
                m_LastError = ex.Message
 
            End Try
        End If
        Return m_Connection
    End Function
 
    ''' <summary>
    ''' Opens the database connection.  Checks first to see if the connection is already open.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub Open()
        If m_Connection.State = ConnectionState.Closed Then
            m_Connection.Open()
        End If
    End Sub
 
    ''' <summary>
    ''' Closes the database connection.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub Close()
        m_Connection.Close()
    End Sub
End Class

Open in new window

Visual Basic ClassicVisual Basic.NETMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
dublingills

8/22/2022 - Mon