-
Nov 20th, 2014, 01:58 PM
#1
Thread Starter
Hyperactive Member
Pass Variables To App.Config File?
I want to use the app.config file to store a connection string. I want to open a database based off a users input into a text box on a windows form. But I get multiple compile errors when I try to input a "+" sign in my appconfig file.
This is what I have, where dbName would be the variable name that would need to be passed into the app.config file based off the users input.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear/>
<add name="AccessDB"
providerName ="System.Data.ProviderName"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\Database\\" + dbName ="+ "\\" + dbName + ".mdb;"/>
</connectionStrings>
</configuration>
Is it possible to pass a variable into the app.config file to perform such a task?
-
Nov 20th, 2014, 05:25 PM
#2
Re: Pass Variables To App.Config File?
The config file is XML, not C#. You can't put C# code into an XML file and expect it to work.
If you want to store a connection string in the config file then you have to store a connection string in the config file, not some C# code that could build a connection string if it was executed. If you want to change the file name in the connection string at run time then you have to actually do that in your C# code, not in the config file. The config file simply stores the value. You get that value at run time and, if you want to change it then, you do so.
-
Nov 23rd, 2014, 03:39 PM
#3
Re: Pass Variables To App.Config File?
What you could do is build the connection string as a app-setting in the app.config where you replace the db-name with {0}. In the C# code you can use string.format to get the correct connectioing string based on the user input
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|