• We just launched and are currently in beta. Join us as we build and grow the community.

How to Change Connection String at Run Time

snzh

OVA Curator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
60
Likes
93
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
One of the challenging parts in VB.NET is to change the connection string of your database location when you run your application.

This tutorial will help you solve this problem. This will apply only to SQL Server but it can be also modified easily to work with MS Access and other DBMS.

In my case I initialize this on the Form Load event on my Login form.

  1. Private

    Sub

    frmLogin_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. ReadfromXML(

    )
  3. MySettingsChanger.

    SetConnectionString

    (

    cnString)

  4. End

    Sub

ReadfromXML is a procedure that reads the value from data.xml and assigns the value to cnString variable.

  1. Public

    Sub

    ReadfromXML(

    )
  2. Dim

    I As

    Integer

  3. For

    I =

    0

    To

    2
  4. Dim

    doc As

    XmlDocument =

    New

    XmlDocument(

    )
  5. doc.

    Load

    (

    "data.xml"

    )

  6. Dim

    root As

    XmlElement =

    doc.

    DocumentElement

  7. ServerName =

    root.

    Attributes

    .

    Item

    (

    0

    )

    .

    Value
  8. DatabaseName =

    root.

    Attributes

    .

    Item

    (

    1

    )

    .

    Value

  9. cnString =

    "Data Source="

    &

    ServerName &

    ";Initial Catalog="

    &

    DatabaseName &

    ";Persist Security Info=True;User ID=sa;Password=yourpasswordhere"

  10. Dim

    CN As

    SqlConnection
  11. CN =

    New

    SqlConnection

  12. Try
  13. With

    CN
  14. If

    .

    State

    =

    ConnectionState.

    Open

    Then

    .

    Close

    (

    )

  15. .

    ConnectionString

    =

    cnString
  16. .

    Open

    (

    )

  17. Exit

    For
  18. End

    With
  19. Catch

    ex As

    Exception
  20. If

    Err.

    Number

    =

    5

    Then
  21. MsgBox

    (

    "Cannot connect to server. Make sure that the server is running. "

    &

    vbCrLf &

    vbCrLf &

    "Otherwise please check for the configuration."

    , MsgBoxStyle.

    Exclamation

    )

  22. Dim

    DBPath As

    New

    frmDBPath

  23. DBPath.

    ShowDialog

    (

    )
  24. End

    If
  25. Finally
  26. CN.

    Close

    (

    )
  27. End

    Try
  28. Next

    I
  29. End

    Sub

Here’s the value of data.xml file.

  1. <?

    xml version=

    "1.0"

    ?>
  2. <

    database server=

    "computer_name"

    name=

    "database_name"

    >
  3. </

    database>

If the connection is successful it will call the MySettingsChanger Class and assign the value to SetConnectionString procedure.

  1. Public

    Class

    MySettingsChanger
  2. Public

    Shared

    Sub

    SetConnectionString(

    ByVal

    cnnString As

    String

    )
  3. My.

    Settings

    .

    RunTimeConnectionString

    =

    cnnString
  4. End

    Sub
  5. End

    Class

You can create a class MySettingsChanger under the Project menu then “Add Class” and paste the above code in the code window.

cnnString will then pass its value to RunTimeConnectionString which can be found under the Project settings.

project_settings.jpg

Here’s the complete code after you click the View Code:

  1. Namespace

    My

  2. 'This class allows you to handle specific events on the settings class:
  3. ' The SettingChanging event is raised before a setting's value is changed.
  4. ' The PropertyChanged event is raised after a setting's value is changed.
  5. ' The SettingsLoaded event is raised after the setting values are loaded.
  6. ' The SettingsSaving event is raised before the setting values are saved.
  7. Partial

    Friend

    NotInheritable

    Class

    MySettings
  8. Public

    WriteOnly

    Property

    RunTimeConnectionString(

    )

  9. Set

    (

    ByVal

    value)
  10. My.

    Settings

    (

    "ADC_DataConnectionString"

    )

    =

    value
  11. End

    Set
  12. End

    Property
  13. End

    Class
  14. End

    Namespace

Some of the code above is generated automatically. Only the RunTimeConnectionString Property has been set here.

 

452,496

338,535

338,543

Top