To define a connection string for ADO using a configuration file

Source: Internet
Author: User
Tags connectionstrings

Recently, I have been learning about the knowledge of ADO, and found that you need to create a connection string for the database operation:

String constr = "Data source= (local); Initial catalog=hospitalinstrument; User id=sa;password=123 ";

It's pointless to feel this repetitive work, and every time you modify the connection string, you have to modify the code to make it easier to maintain the application.

In fact, the best way is to write the connection string in the app's config file, app. So how do I add a configuration file? This is just like adding a new class in the project, right-clicking the current project in Solution Explorer → add → new item → general → application configuration file. App. Config is a common configuration file for. NET, as well as in ASP.

Application configuration file App. config is a standard XML file, and XML tags and attributes are case-sensitive. It can be changed as needed, and developers can use the configuration file to change the settings without having to recompile the application. The root node of the profile is the configuration file. All we need to do here is add a connectionstrings configuration section to it, and add an additional item to it. Use the Name property for a name (for example, Constr), and the ConnectionString property to specify the connection string.

Here's how:

(1) Add "application configuration File"

(2) Add the configuration code in the configuration file

The ConnectionString is the connection string to configure, where data source represents the server, "." The representative is a local server, if ". SQLExpress" means that the SQL Server you are using is an express version, the local server can also use "(local)", and if it is not a local server, set the data source to the IP address of the server. Initial catalog is the database name, and the connection authentication method used here is SQL Server authentication, so set the user ID and password.

Next, read the connection string in the configuration file in the program:

   to read a connection string in a configuration file using the ConfigurationManager class

Using the ConfigurationManager class to read the configuration file, first refer to the namespace where the class resides in the current project.

After you add a reference, you can use System. The ConfigurationManager class under the configuration namespace.

String constr = configurationmanager.connectionstrings["instrument"]. connectionstring;//Gets or sets the connection string in the configuration file

After the above configuration, you will never have to repeat the copy and paste the connection string, even if the IP address of the server later changed, it is only necessary to modify the database in the configuration file IP is good.

To define a connection string for ADO using a configuration file

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.