.Net 2.0的配置文件中新增加了一个节点<connectionStrings>。这个节点是专门用于存储数据库联接字符串的。不尽包含了字符串而且还包含了使用何种Provider的信息。
例如:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings>
<add name="NorthwindConnectionString1" connectionString="Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
<configuration />
System.Configuration.ConnectionStringSettings s = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"];
DbProviderFactory f = DbProviderFactories.GetFactory(s.ProviderName);
using (DbConnection conn = f.CreateConnection())
{
conn.ConnectionString = s.ConnectionString;
conn.Open();
DataTable t = conn.GetSchema("Tables");
t.WriteXml("tables.xml");
}
}