0

The program I'm writing accesses a database. So when I use the SqlConnection() class, I hard code the actual connection string as a parameter. Eventually I'd like o deploy this program to different users. So my question is:

When a user installs a program on their computer, how does the new connection string get created, where is it stored, and how can I access it?

Thanks for the help

Bob Gatto
  • 131
  • 8
  • Since you are writing the program, you may choose and design it. It does not happen automatically. – Silas Reinagel Mar 03 '16 at 16:55
  • C# has a few drawbacks where connection strings are concerned. Try looking at this question. http://stackoverflow.com/questions/17936392/mysql-securestring-as-connection-string/18150784#18150784 – Kassabba Mar 03 '16 at 19:04

2 Answers2

2

You need to out it in a configuration file and load it from there. For an ASP.NET application it would be in the web.config file;

 <connectionStrings>
    <add name="MyConnection" connectionString="MyConnectionString" />
 </connectionStrings>

and then use

string connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString

in your application.

The for each installation it would be configured for the local requirements.

For a desktop application the details are different but the principle is the same.

See references in answer from Luis Sagasta

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
1

As explained in MSDN, you should save the connection string in the configuration file: MSDN: Connection Strings and Configuration Files

In the same article you will find information about encrypting the configuration section: MSDN:Encrypting Configuration File Sections Using Protected Configuration

Regards.

Luis Sagasta
  • 101
  • 1
  • 6