c#连接不同数据

2021-02-15 03:17

阅读:613

配置如下


...

providerName="System.Data.SqlClient"
connectionString="server=(local);integrated
security=SSPI;database=Northwind" />

 

连接代码如下

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;

这是比较通用的数据库连接方法,如果需要切换数据库,可以使用上述的代码


评论


亲,登录后才可以留言!