连接字符串配置文件中存储
SqlConnection conn = new SqlConnection();
//手写
//string connStr = "server=.;database=TestBase;uid=lyc;pwd=123456";
//SqlConnectionStringBuilder生成连接字符串
//SqlConnectionStringBuilder connStrBuilder = new SqlConnectionStringBuilder();
//connStrBuilder.DataSource = ".";//设置数据源
//connStrBuilder.InitialCatalog = "TestBase";//数据库名
//connStrBuilder.UserID = "lyc";//账号
//connStrBuilder.Password = "123456";//密码
//connStrBuilder.Pooling = false;//禁用连接池
//string connStr = connStrBuilder.ConnectionString;
//读取配置文件里的连接字符串
//配置到文件中 写哪个位置? (推荐) connectionStrings节点中 appSettings节点中
//string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;//读取连接字符串
string connStr = ConfigurationManager.AppSettings["connStr"].ToString();
conn.ConnectionString = connStr ;
//连接字符串如果任何一个元素设置不正确,都不能正常打开conn
conn.Open();
conn.Close();
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name ="connStr" connectionString="server=.;database=TestBase;uid=lyc;pwd=123456" providerName ="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key ="connStr" value ="server=.;database=TestBase;uid=lyc;pwd=123456"/>
</appSettings>
</configuration>