ado.net – ASP.NET:如何从web.config ConnectionString创建一个连接?

如何根据提供者名称构建DbConnection?

示例提供商名称

> System.Data.sqlClient
> System.Data.OleDb
> System.Data.Odbc
> Firebirdsql.Data.FirebirdClient

我有连接字符串存储在我的IIS服务器的web.config文件中:

<connectionStrings>
  <add name="development"
        connectionString="Provider = IBMDA400; Data Source = MY_SYstem_NAME; User Id = myUsername; Password = myPassword;" 
        providerName="System.Data.OleDb" />
  <add name="live" 
        connectionString="usd=sa;pwd=password;server=deathstar;" 
        providerName="System.Data.Odbc" />
  <add name="testing" 
        connectionString="usd=sa;pwd=password;server=deathstar;" 
        providerName="System.Data.sqlClient" />
  <add name="offline"
        connectionString="Server=localhost;User=SYSDBA;Password=masterkey;Charser=NONE;Database=c:\data\mydb.fdb"
        providerName="Firebirdsql.Data.FirebirdClient"/>

你可以看到他们都使用不同的提供者.当我来创建一个连接的时候,我必须知道要创建什么样的DbConnection,例如:

> sqlConnection
> OleDbConnection
> OdbcConnection
> FbConnection

connectionStrings条目包含一个providerName,但这些不是DbConnection后代类的名称,而是一个命名空间

我如何根据字符串providerName构建一个DbConnection?

public DbConnection GetConnection(String connectionName)
{
    //Get the connectionString infomation
    ConnectionStringSettings cs = 
          ConfigurationManager.ConnectionStrings[connectionName];
    if (cs == null)
       throw new ConfigurationException("Invalid connection name \""+connectionName+"\");

    //Create a connection based on the provider
    DbConnection conn = new DbConnection();

}

解决方法

如果你走这条路线,我想你会想要使用DbProviderFactories类获得一个可以用来构建连接的DbProviderFactory.我没有尝试过这个代码,但我认为它会奏效.您可能需要使用DbProviderFactories类上的GetFactoryClasses方法查找提供程序名称,并使用InvariantName.
public DbConnection GetConnection(String connectionName)
{
   //Get the connection string info from web.config
   ConnectionStringSettings cs= 
         ConfigurationManager.ConnectionStrings[connectionName];

   //documented to return null if it Couldn't be found
   if (cs == null)
      throw new ConfigurationErrorsException("Invalid connection name \""+connectionName+"\"");

   //Get the factory for the given provider (e.g. "System.Data.sqlClient")
   DbProviderFactory factory = 
         DbProviderFactories.GetFactory(cs.ProviderName);

   //Undefined behavIoUr if GetFactory Couldn't find a provider.
   //Defensive test for null factory anyway
   if (factory == null)
      throw new Exception("Could not obtain factory for provider \""+cs.ProviderName+"\"");

   //Have the factory give us the right connection object      
   DbConnection conn = factory.CreateConnection();

   //Undefined behavIoUr if CreateConnection Failed
   //Defensive test for null connection anyway
   if (conn == null)
      throw new Exception("Could not obtain connection from factory");

   //KNowing the connection string,open the connection
   conn.ConnectionString = cs.ConnectionString;
   conn.open()

   return conn;
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...