首页 > 代码库 > 数据库连接字符串

数据库连接字符串

数据库连接

1、在文件App.config内添加连接

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3   <connectionStrings> 4     <!--数据库认证--> 5     <add name="Database1ConnectionString" 6          connectionString="Data Source=DBServerName1;Initial Catalog=AdventureWorks2008;User ID=sa;Password=abc123" 7          providerName="System.Data.SqlClient" /> 8     <!--Windows集成认证--> 9     <add name="Database2ConnectionString"10          connectionString="Data Source=DBServerName2;Initial Catalog=Northwind;Integrated Security=True"11          providerName="System.Data.SqlClient" />12 </configuration>

2、添加 dll 引用

 

3、获取数据库连接字符串

string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ToString();