Read the App. config data connection string from the winform. cs File

Source: Internet
Author: User
Tags connectionstrings
We put the data connection string in the configuration file, but how to read it was previously used in vs2003
Configurationsettings. deleettings ["connectionstring"]. tostring ();
To read the configured connection, but the compiler prompts in vs2005:
Warning "system. configuration. configurationsettings. deleettings" is out of date:
"This method is obsolete, it has been replaced by system. Configuration!
System. configuration. configurationmanager. deleetmanager ".
So I want to find the configurationmanager class. The result is that I have referenced system. Configuration and cannot find it,


Later, I found out that the system. configuration. dll file must be added to the reference.
Sure enough, you can use the following statement to read the configuration after adding it:
System. configuration. configurationmanager. connectionstrings ["connectionstring"]. tostring ();

However, if you do not add a reference to the system. Configuraton. dll file, you can use the following method to read the data connection string in the configuration.
First, paste the content of my app. config file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
</ConfigSections>
<ConnectionStrings>
<! -- Here we add an oracle data connection string -->
<Add name = "TestAdoNet2.Properties. Settings. ConnectionString"
ConnectionString = "Data Source = gz; Persist Security Info = True; User ID = gzmes; Password = gzmes; Unicode = True"
ProviderName = "System. Data. OracleClient"/>

<! -- Here we add an SQL data connection string -->
<Add name = "TestAdoNet2.Properties. Settings. NorthwindConnectionString"
ConnectionString = "Data Source =. \ SQLExpress; Initial Catalog = Northwind; Integrated Security = True"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
</Configuration>

The code for getting the connection string is as follows:
// Oracle
String conn1 = Properties. Settings. Default. ConnectionString;
// SQL
String conn2 = Properties. Settings. Default. NorthwindConnectionString;

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.