app.config 配件文件信息如下
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="Conn" connectionString="Data Source=127.0.0.1;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test;Min Pool Size=1;Max Pool Size=10;Connect Timeout=30;Load Balance Timeout=60;ConnectRetryCount=10" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="MySqlCon" value="Data Source=localhost;Port=3306;Database=lgee_shop;User Id=root;Password=root;CharSet=utf8mb4;" /> </appSettings> </configuration>
第一步
引用 using System.Configuration;
若有问题,需要在解决方案管理器中找到“引用”,点击右键“添加引用”找到System.Configuration即可。
第二步:读取配置文件相关数据
读取节点数据:appSettings
string appCon = ConfigurationManager.AppSettings["MySqlCon"];
读取节点数据:connectionStrings
string conMySql = ConfigurationManager.ConnectionStrings["Conn"].ToString();