Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

sql server connection string in config file

I have a windows app and I need to use a config file so that I can have the connection strings in that, in my app some are hard coded and some are using the connection manager in 1 instance.

Ive noticed that visual studio has already created the config file for me. I dont know when or how but its there

I need to remove all instance of the hardcoded strings that I have. As the config file is here already how do I make it load these from the config at startup?

and what do I place in my code as the substitute to the string?

I need to do this as I need to deploy the app now to a colleague. I am working offsite but he will still use the windows log on detail.

I have this in a file called app.config which is in the project folder in the folder the same name as the project.

my project setting look like:User generated image
my project is a little erratic as ive been learning and using different ways so i have this as one way to dictionary:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

Open in new window

and other ways has the string in its entirety

strangely another way here
Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")

Open in new window




<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <connectionStrings>
    <add name="Dictionary" connectionString="Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True"
      providerName="System.Data.SqlClient" />
    <add name="WindowsApplication1.My.MySettings.DictionaryConnectionString"
      connectionString="Data Source=MAIN-PC;Initial Catalog=Dictionary;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="WindowsApplication1.My.MySettings.ABI_MASTEROleConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;N:\Data\Abi\ABI MASTER.mdb&quot;"
      providerName="System.Data.OleDb" />
    <add name="WindowsApplication1.My.MySettings.SMMT_MasterOleConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;|DataDirectory|\SMMT Master.mdb&quot;"
      providerName="System.Data.OleDb" />
    <add name="WindowsApplication1.My.MySettings.Glass_MasterOleConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;N:\Data\Glass\ABI MASTER.mdb&quot;" />
    <add name="WindowsApplication1.My.MySettings.Tvi_MasterOleConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;N:\Data\Thatcham\TVI Master 4.mdb&quot;" />
    <add name="WindowsApplication1.My.MySettings.Cap_MasterOleConnectionString"
      connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;N:\Data\Cap\CAP MASTER.mdb&quot;" />
    <add name="WindowsApplication1.My.MySettings.ClientDataConnectionString"
      connectionString="Data Source=MAIN-PC;Initial Catalog=ClientData;Integrated Security=True" />
  </connectionStrings>
</configuration>

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

since you have:

Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

Open in new window


then try use this in your connection object:
       Using cnSql As New SqlClient.SqlConnection(connectionString)
             '....
        End Using

Open in new window

Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

Youve misunderstood. Ive altered the code now so that I refer and connect iusing as you say:

Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

Open in new window


all that works and my windows app is finished.

so on startup does the windows app automatically refer to the app.config file?

and if it does I assume that if at my clients end they can change the config file sql server and locations and my program will work and make the connections from the app.config file?
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cool so it wasnt as bad as I thought, thx