Connection String problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a winform application. The backend database is MS Access.

I don't want to hardcode the database connectin string in my application.
What is the best practice to configure the database connection string in .Net
application ?

Thanks!
 
You should put that in the config file, which would be exename.config (just
an xml file, nothing fancy, except it is the standard way of putting config
info, connection strings inclusive) in that. You could then encrypt it too.

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
Hi Tracey,

With .NET, we have XML based configuration files within which we store the
application settings. These files have the format <app name>.exe.config or
web.config etc.
Add an config file into your solution (right click on the project and click
Add->app config file). This file gets automatically buit when you build the
app. Then you can use the ConfigurationSettings class to access the values
you have set.

Go thru this MSDN article for an example
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondeclaringcustomconfigurationsections.asp

PS: Apart from the app.config files, you could use your own config files
too, if you need to.

HTH,
Rakesh Rajan
 
U¿ytkownik "Tracey said:
I have a winform application. The backend database is MS Access.

I don't want to hardcode the database connectin string in my application.
What is the best practice to configure the database connection string in
.Net
application ?

If you don't plain to encrypt the connection string, you can use Data Link
file - simply make empty file, change extension to udl, open it and build
appropriate connection string. Then in your application use:

OleDbConnnection cn = new OleDbConnection("File Name=MyData.udl;");

Regards,
Grzegorz
 
Thanks. I'm not clear on the application path issue. Suppose my app is
deployed on client's machine. In my code file, must I concatenate the
application path with the connection string fetched from the config file
each time?
 
So say, your application name is Tracey.exe, the config file will be
Tracey.exe.config in the same directory as your exe.

Also, you don't need to do anything with the application path as such, you
need to instead look at the System.Configuration namespace.

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
I believe I'm on the right track.
Also, you don't need to do anything with the application path as such, you
need to instead look at the System.Configuration namespace.

What do you mean by I don't need to do anything with the application path ?
Can you show a block of sample code ? thx.
 
Back
Top