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

So here it is. I just created and "published" a personnel management tool in Visual C #. During development, I used the string stored in Properties.Settings.Default to connect to the database that I used for development. Now that the solution is published and ready to go, the boss wants to connect to a real staff database. I got the impression that connecting to the new database would be as simple as changing the connection string in any properties file. Unfortunately, I cannot find a suitable file / line to connect to the database that I want. Any ideas?

Thank! Jb

+3
source share
2 answers

Look at here:

Connection strings and configuration files

Using the configuration file, you just need to change the connection string to the configuration file after deploying your application.

Here is a way to do what you want:

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

The contents of your configuration file:

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

The way 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");
+2
source

Properties.Settings, , . , .config ConnectionStrings, AppSettings node. , , ...

0

Source: https://habr.com/ru/post/1752932/


All Articles