DYNAMIC CONNECTION STRINGS IN VB.NET

To change the connection string of a typed dataset in vb.net 2005 and also Building a dynamic connection strings in Vb.net. Below are easy ways these can be done.

1. To change the connection string of a typed dataset, add the method below to the setting file. To see the setting.vb file, go the your project properties, on the settings tab, click on the the view code button and there you go.

Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

Dim db As New DBConnect

Me.Item("HospitalConnectionString")  =db.returnConnectionString

End Sub

Note: HospitalConnectionString is the name of the connections string as saved in your project settings

Below is the detail of the “returnConnectionString” procedure

Also note that servername, username, database, password are all saved in the settings

Public function returnConnectionString ()as string

Try

client = getClientName()

With My.Settings

If .servername = client Then

If Me.integratedSecurity = False Then

connectionString = "Data source= " & .servername & "; initial catalog= " & .database & ";user id= " & .username & " ;password = " & .password

Else

connectionString = "Data source= " & servername & "; initial catalog= " & database & "; Integrated Security= " & integratedSecurity

End If

Else

connectionString = "workstation id= " & client & " ;Data source= " & servername & " ;initial catalog= " & database & " ;user id= " & username & " ;password = " & password & " ;Integrated Security=" & integratedSecurity & " ; Connect TimeOut=30"

End If

End With

Catch ex As Exception

Msgbox(ex.ToString)

End Try

Return connectonString

End function

——————————

A function to return the name of the current machine(the machine the program is running on)

Public Function getClientName() As String

Dim client As String = ""

client = Dns.GetHostName

Return client

End Function

Saving items in the Settings

Below is code under the save setting buttons

With My.Settings

.Server = Me.txtServer.Text

.Dbase = Me.txtDbase.Text

.UserName = Me.txtUserName.Text

.Password = Me.txtPassWord.Text

.integrated = Me.ChkSecurity.Checked

.Save()

End With

Tags: , , , ,

Advertisements

8 thoughts on “DYNAMIC CONNECTION STRINGS IN VB.NET

  1. i m using one text file for providing database name and server name can i use this file for providing dbname and server name…to change dataset connection string

    • Yea, its possible, u read those values from your file on the “settings.vb” form
      load event.
      You can then configure the connectionstring for your dataset in that same event

      To see the settings.vb file, right click on your project, select properties, check for the settings tab and then click on the view code button

  2. i m using one text file for providing database name and server name can i use this file for providing dbname and server name…to change dataset connection string …

  3. Hi Gilbert,
    Your post on dynamic connection string looks good. However the settings fIle seems to run before showing the form. So I can’t enter data before it runs. As such it gives an error because it doesn’t get data. Please help. How does one connect to sql using DBConnect,if at all? Tnx

    • the DBconnect is a class i created. what i do is I check the connection state, if it is not successful , i show a pop up where the user get to enter the connection parameters. These entered parameters are saved in the settings file. will send u the copy of the class to your inbox

  4. Hello Gilbert.
    I am using visual studio 2010 from statement “Dim db As New DBConnect” in your solution I didnt find class DBConnect hence It gives error

    • Hi, Dbconnect is a user defined class that only has the function returnConnectionString defined it it. so u can declare a new class with the name dbcoonect and copy the function as shown in the post into it.

  5. Hello Again, Mr Gilbert.

    Thanks for your advice it help to eliminate error defining dbConnect, it is seems like i got more challenge than before since I am not pro in programming can u please help me my sending codes as appear in project
    Sorry my English is not good

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s