(4) C # ADO to put the database connection string into the configuration file

Source: Internet
Author: User
Tags connectionstrings

Given the security of the code and the ease with which it has been modified, we often do not place the database connection string directly in the code, but instead place the string in an app. config configuration file, assign it to a variable, and then reference the variable in the code.

Here's how to do this: ① right-click the project name and create a new application configuration file that is renamed App. Config. ② Add the following code in app. Config
1 <configuration>2   <connectionStrings>3     <add name=" Myconnstr " connectionstring="Data source=pc201507182002\sqlexpress;initial catalog=123456;integrated Security=true"/>4   </connectionStrings>5 </configuration >

③ add a name for theSystem.ConfigurationReference, and then change the original code to create the connection string to the following.
      string connstr = configurationmanager.connectionstrings["connstr"]. ConnectionString;             using New SqlConnection (CONNSTR))

Note: In the above code, the green ConnStr is the name value of add in the config File App. Config.

(4) C # ADO to put the database connection string into the configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.