Main Topics
Browse All TopicsHi,
I am connecting to a few different databases in my C# .NET 2.0 winform application.
I will obviously have different dataserver names, users and passwords etc. Where is the best place to store this within the c# application. How do I subsequently retrieve the values via code?
Thanks as ever
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks for the above.
I might be doing this wrong - sorry if I picked it up wrong. But my concern with this approach is that when I build the release of my winform application these config details will be in my exe.config file and viewable to the world. My user name and password to databases will be there. The purpose of the application is to provide limited interaction with the database. I don't want them to be able to access the config details and manipulate the underlying database.
so right now my app.config has the below:
<connectionStrings>
<add name="DEV"
connectionString="Driver={
providerName="System.Data.
</ connectionStrings>
If I'm to go with this approach I'd need to be able to hide this somehow?
Sorry to complicate things!
You have a couple of options:
1. Store the connection string as separate entities in the app.config file:
<appSettings>
<add key="DataSource" value="ServerName"/>
<add key="InitCatalog" value="DBName"/>
<add key="UserName" value="[EncryptedUserName]
<add key="Password" value="[EncryptedPassword]
</appSettings>
and then build your connection string in code after decrypting the username and pwd values.
2. Create a public class called ConnectionStrings.cs and store all of your connectionstrings as static string members of the class. The downside of doing this is that if any of the parameters of the connectionstring change, you'll have to re-compile your code.
Arun
nmarun, thanks for the rapid reply.
sorry, but i'm going to throw some more questions at you.
Option 2 seems like a non-runner. So lets focus on option 1
As I mentioned I've multiple database config to store between prod, uat, dev etc. If I use <appSettings>
(1) Will I need to do something like:
<add key="DEV1DataSource" value="ServerName"/>
<add key="DEV1InitCatalog" value="DBName"/>
<add key="DEV1UserName" value="[EncryptedUserName]
<add key="DEV1Password" value="[EncryptedPassword]
<add key="DEV2DataSource" value="ServerName"/>
<add key="DEV2InitCatalog" value="DBName"/>
<add key="DEV2UserName" value="[EncryptedUserName]
<add key="DEV2Password" value="[EncryptedPassword]
<add key="DEV3DataSource" value="ServerName"/>
<add key="DEV3InitCatalog" value="DBName"/>
<add key="DEV3UserName" value="[EncryptedUserName]
<add key="DEV3Password" value="[EncryptedPassword]
Doesn't seem very tidy
(2) How will I call this from code?
Thanks again. Sorry to be a pest :-S
Business Accounts
Answer for Membership
by: spprivatePosted on 2009-09-11 at 11:01:40ID: 25311960
Configuration file.
tionSettin gs.AppSett ings["Data source"].T oString();
App.config
connectionstring=Configura