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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pick Connection String from App.Config at run time and use throughout application

Status
Not open for further replies.

jeffshex

Technical User
Jun 30, 2005
208
US
I'm wondering what the best way to do this would be:
I am trying some stuff out and have a server with 2 databases on it, Live and Test. Basically I want to pick a connection on the first form that gets used throughout the program until it is closed. Everytime you run the program, you have to pick one.
I have two connection string settings in my app.config for each of those databases.
On the startup form I can pull the connection strings from the configuration manager and display them in a combobox. After that, I can click a button and get the connection string from the manager.

My question then arises: Where and how would I store that connection string that I can pull on the startup form? Would I store it in the application properties somewhere or assign it to somewhere global?

I'm still pretty new to coding in this but just need some direction here.
Thanks!
 

One way to do it would be:
In Solution Explorer, double click My Project and go to Settings. In Name type: MyTest, Type: Connection String, Scope: Application, Value - set it to connect to your test.
Do the same again and call it MyLive (connect to your Live DB).

On the first form, I put 2 radio buttons, Connect button, and Exit button. I also have a Module where my connection (and Exit) is placed.

(I connect to Oracle, but the logic is the same if you connect to anything)

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles MyBase.Load
        radText.Checked = True
    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles cmdConnect.Click

        Select Case True
            Case radText.Checked
                MyConnection = My.Settings.MyTest
            Case radlive.Checked
                MyConnection = My.Settings.MyLive
        End Select

        Call OpenConnection()

    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles cmdExit.Click
        Call ExitAll()
    End Sub
End Class

Code:
Imports Oracle.DataAccess.Client

Module Module1
    Public MyConnection As String = ""
    Friend Cn As OracleConnection

    Public Sub OpenConnection()
        Cn = New OracleConnection(MyConnection)
        Cn.Open()
    End Sub

    Sub ExitAll()
        If Cn.State = ConnectionState.Open Then
            Cn.Close()
        End If
        Application.Exit()
    End Sub
End Module

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top