c#連接不同數據
阿新 • • 發佈:2018-02-11
ret tin ssp settings name ger provider 數據 onf
配置如下
<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;
這是比較通用的數據庫連接方法,如果需要切換數據庫,可以使用上述的代碼
c#連接不同數據