C # WinForm Program app.config database connection configuration file use process

Source: Internet
Author: User
Tags config connectionstrings

App.config "Application configuration file", it is actually a standard XML file, but. NET class library has encapsulated the method of reading this file. can be very convenient to use. Look at the use process.

1. Right-click your project name in Solution Explorer, add > New Item, select application configuration file in the list, and the default file is app.config. Are you sure.

2. Open app.config, the initial XML code is:

<xml version= "1.0" encoding= "Utf-8" >  
<configuration>  
</configuration>

Set your connection string in the following format

<xml version= "1.0" encoding= "Utf-8" >  
<configuration>  
    <connectionStrings>  
        <!-- The server here can write (local) or write a dot .-->  
        <add name= "sqlconnstring connectionstring=" server=.; database= database name; uid= user name; pwd= password "  />  
    </connectionStrings>  
</configuration>

Where name is the identity name of the connection string (similar to an alias). ConnectionString is your connection string. You can write multiple add nodes.

Note: The format is fixed

3. Right-click "Reference" in your project in Solution Explorer, add System.Configuration Reference. In your code, you need both:

Using System.Configuration;

4. began to acquire the

String connstr= configurationmanager.connectionstrings["MyConn"]. ConnectionString;  
 SqlConnection cn = new SqlConnection (CONNSTR);  
 cn. Open ();

At this point the database is open, you can write the appropriate code

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.