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

2
  • Since you are writing the program, you may choose and design it. It does not happen automatically. Commented Mar 3, 2016 at 16:55
  • C# has a few drawbacks where connection strings are concerned. Try looking at this question. stackoverflow.com/questions/17936392/… Commented Mar 3, 2016 at 19:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.