I am working on a VS2010 Winform (not ASP web form). I have a copy of the local database on my computer. In this application, I have 3 forms in my application that need to connect to the same database, but different tables. Right now I am defining my connection string in each form something like this:

private string ConnString = "Data Source=(local);Initial Catalog=Inspections;Persist Security Info=True;User ID=MyUser;Password=MyPassword";

After I work on my project locally, I need to change it to use the real database on one of our SQL servers, so I change the string in all three forms. I have been reading on ConfigurationManager.ConnectionStrings but the way it sounds, it is for ASP/Web applications using a web.config file to store database strings? Then I read another page on adding a new item, a Settings File, under Project --> Add New Item... --> Settings File. I was able to set up both of my database strings. How do I call them or retrieve them "globally"? I tried something like:

string conn = Properties.Settings.Default.;

but stopped there because my string name is not showing up after the "Default" property.

Thanks!
Rick

Recommended Answers

All 2 Replies

you have app.config file where u can place connection strings so that they can be reffered in multiple forms later(globally retrieved)

If you want to be able to switch to a particular database depedent on a particular condition you could use a case statement withing your DB connection method which references at spacific connection string dependent on the case (if statements will work too).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.