I'm writing a database application in C#. I need to place the db connection in one location and call it once I need in several windows forms. I'd be thankful , if you can help me.
-
1take a look here: http://stackoverflow.com/questions/9972946/how-to-store-connection-string-in-winforms-application – eMi Oct 26 '12 at 06:35
2 Answers
A common way to do this task is to store the connection string in the app.config (web.config in a web application), either as a connection string or an application setting
Have a look at: System.Configuration.ConfigurationManager or this article
"several windows forms" -> However it sounds like you want to share the same connection string between different applications, perhaps on different machines so you have only one place to modify such data.
You will then need some kind of central repository or configuration server to do this task... for example, a web service that supplies the connection string to the various applications.
You would need to lock down the network for such a configuration server so that it is not exposed to dodgy people both on the internet or internally in your organisation.
You would need to cache to connection string per application somewhere after the application loads (static class, singleton.. etc..) so that the central configuration does not become a single point of failure.. if the configuration server goes down you will still want to connect to the last known good config.. also you would not want to constantly query for the connection string because it would be way slow.
This might be overkill for what you need, it might be as simple as reading a common file off disk or deploying the same .config to each application
There are many ways to solve this problem, choose the simplest one with the least moving parts.
You can use Application Settings in Windows Forms to store connection string.
For details you can see this walk through.

- 13,010
- 3
- 33
- 42