代码之家  ›  专栏  ›  技术社区  ›  Ian Boyd

ASP.NET:如何从web.config connectionstring创建连接?

  •  10
  • Ian Boyd  · 技术社区  · 15 年前

    如何基于 提供者名称 ?

    样品 提供者名称 S

    • 系统.data.sqlclient
    • 系统数据.oledb
    • 系统.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"/>
    

    你可以看到他们都使用不同的提供者。当我需要创建连接时,我必须知道要创建哪种类型的数据连接,例如:

    • 连接对象
    • OLED连接
    • ODBC连接
    • FB-连接

    connectionStrings条目包含 商号 ,但这些不是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();
    
    }
    
    3 回复  |  直到 15 年前
        1
  •  15
  •   Ian Boyd    15 年前

    如果您走这条路线,我想您将要使用DBProviderFactories类来获取一个可以用来构造连接的DBProviderFactory。我还没有尝试过这个代码,但我认为它会起作用。可能需要使用dbProviderFactories类上的getFactoryClasses方法查找提供程序名称,并使用不变量名。

    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;
    }
    
        2
  •  0
  •   blu    15 年前

    退房 this Hanselman blog 在为不同的连接字符串名称添加自定义生成类型时,看起来它可能与使用提供程序类型的方式不同,适合您想要完成的任务。

        3
  •  0
  •   Marc    15 年前

    如果特定连接名(dev、test、prod)的providername从未更改过,为什么不能为方法打开connection name参数并以这种方式设置providername实例?