Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect published Visual C# solution to a different database

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

like image 725
JnBrymn Avatar asked Mar 20 '26 09:03

JnBrymn


1 Answers

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");
like image 186
Leniel Maccaferri Avatar answered Mar 23 '26 00:03

Leniel Maccaferri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!