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:
my project is a little erratic as ive been learning and using different ways so i have this as one way to dictionary:
strangely another way here
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:
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
and other ways has the string in its entiretystrangely another way here
Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
<?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="N:\Data\Abi\ABI MASTER.mdb""
providerName="System.Data.OleDb" />
<add name="WindowsApplication1.My.MySettings.SMMT_MasterOleConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="|DataDirectory|\SMMT Master.mdb""
providerName="System.Data.OleDb" />
<add name="WindowsApplication1.My.MySettings.Glass_MasterOleConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="N:\Data\Glass\ABI MASTER.mdb"" />
<add name="WindowsApplication1.My.MySettings.Tvi_MasterOleConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="N:\Data\Thatcham\TVI Master 4.mdb"" />
<add name="WindowsApplication1.My.MySettings.Cap_MasterOleConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="N:\Data\Cap\CAP MASTER.mdb"" />
<add name="WindowsApplication1.My.MySettings.ClientDataConnectionString"
connectionString="Data Source=MAIN-PC;Initial Catalog=ClientData;Integrated Security=True" />
</connectionStrings>
</configuration>
ASKER
Youve misunderstood. Ive altered the code now so that I refer and connect iusing as you say:
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?
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cool so it wasnt as bad as I thought, thx
Open in new window
then try use this in your connection object:
Open in new window