写在 app.config 里面
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 运行环境 -->
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<!-- 配置连接字符串 -->
<connectionStrings>
<add name="conStr" connectionString="Data Source = WINGEL; Initial Catalog = People; Trusted_connection = SSPI"/>
</connectionStrings>
</configuration>
添加 System.Configuration 引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration; // 引入配置信息
using System.Data.SqlClient;
namespace Dome13_Framework配置文件读取
{
internal class Program
{
static void Main(string[] args)
{
// ConfigurationManager 配置信息管理者 ConnectionStrings[] 获取连接字符串
// 再用 .ConnectionString 转变成string类型
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
Console.WriteLine("连接成功");
}
}
}
}
标签:string,配置文件,ConfigurationManager,System,framework,conStr,using,读取
From: https://blog.csdn.net/2301_79007589/article/details/143269551