C #-How to read the AppConfig file configuration database connection

Source: Internet
Author: User
Tags connectionstrings

The app. config configuration is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<connectionstrings >
<add name= "Strcon" connectionstring= "Server=127.0.0.1;user id=root; password=cms1005; Database=uestc_infosoft; Pooling=false;port=8500 "/>
</connectionStrings>
<appSettings>
<add key= "ConnectionString" value= "Server=127.0.0.1;user id=root; password=cms1005; Database=uestc_infosoft; Pooling=false;port=8500 "/>

<!--add key= "ConnectionString" value= "Data source=127.0.0.1;initial catalog=uestc_infosoft;integrated security= SSPI; " /-->

<add key= "Constringencrypt" value= "true"/>
</appSettings>
</configuration>


Primarily database operations:

Method One: String _connectionstring = system.configuration.configurationmanager.connectionstrings["StrCon"]. ToString ();

Method Two: String _connectionstring = configurationmanager.appsettings["connectionString"];


read in background:
SqlConnection conn = new SqlConnection ();
Conn. ConnectionString = configurationmanager.connectionstrings["Strcon"]. ConnectionString;
Conn. Open ();
String sql = "SELECT * from Orders";
SqlCommand cmd = new SqlCommand (SQL, conn);
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = cmd;
DataSet ds = new DataSet ();
Da. Fill (DS);
This.dataGridView1.DataSource = ds. Tables[0];
Conn. Close ();

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1670205

C #-How to read the AppConfig file configuration database connection

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.