c#连接不同数据
配置如下
<configuration>
...
<connectionStrings>
<add name="Northwind"
providerName="System.Data.SqlClient"
connectionString="server=(local);integrated
security=SSPI;database=Northwind" />
</connectionStrings>
</configuration>
连接代码如下
private DbConnection GetDatabaseConnection (string name)
{
ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings[name];
DbProviderFactory factory = DbProviderFactories.GetFactory
(settings.ProviderName);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = settings.ConnectionString;
return conn;
这是比较通用的数据库连接方法,如果需要切换数据库,可以使用上述的代码