views:

44

answers:

2

So here's the what's up. I just created and "published" a staff management tool in Visual C#. During development, I used a string saved in Properties.Settings.Default to connect to the database I was using for development. Now since the solution is published and ready to go, the boss wants to connect to the real staff database. I was under the impression that connection to the new database would be as simple as changing the connection string in some properties file somewhere. Unfortunately I can't seem to find the proper file/string to connect to the database I want to. Any ideas?

Thanks! JB

+2  A: 

Look here:

Connection Strings and Configuration Files

By using a config file you just have to change the config file connection string once your application has been deployed.

Here's a way of doing what you want:

From http://www.dreamincode.net/forums/topic/70745-connection-string-in-appconfig/

Your config file content:

<connectionStrings >
<add name="YourName"
connectionString="Provider=msdaora;Data Source=MyOracleDB;Persist Security Info=False;Integrated Security=Yes;"
providerName="System.Data.OracleClient" />
</connectionStrings> 

Method to get the connection string at runtime:

public static string GetConnectionString(string strConnection)
{
 //Declare a string to hold the connection string
 string sReturn = new string("");
 //Check to see if they provided a connection string name
 if (!string.IsNullOrEmpty(strConnection))
 {
  //Retrieve the connection string fromt he app.config
  sReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString;
 }
 else
 {
  //Since they didnt provide the name of the connection string
  //just grab the default on from app.config
  sReturn = ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString;
 }
 //Return the connection string to the calling method
 return sReturn;
}

Using the method:

string connectionString = GetConnectionString("YourName");
Leniel Macaferi
Cool. I'll try this out ASAP and get back with you.
John Berryman
Ok,I tried and failed... *but* I'm close. I have the app.config file which refers to the connection strings in the external file via '<connectionStrings configSource="connections.config"/>'. When I'm debugging the code, I can place the connections.config file in the Debug\bin directory and everything works fine. But when I go to publish it, the solution doesn't put connections.config in the proper directory... and I don't know what that directory is either. Where do I place connections.config upon publishing the solution?
John Berryman
Look here: http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx You should copy the file connections.config to the output directory.
Leniel Macaferi
A: 

I've had to change the entry in the Properties.Settings text file, recompile and redeploy to get the new connectionstring to take. In the future consider reading your connection string from your .config file under either the ConnectionStrings or AppSettings node. When you store it there you simply need to change a production text file to switch your database...

Tahbaza