你怎么建造一个对象的基础上 供应商的名字?

样本 供应商的名字s

  • 系统。数据。SqlClient
  • 系统。数据。OleDb
  • 系统。数据。Odbc
  • FirebirdSql.数据。FirebirdClient

我已经连串的存在我的IIS服务器的网络。配置文件:

<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"/>

你可以看到它们都使用不同的供应商。当时候对我来创建一个方面,我必须知道什么样的对象的创造,例如:

  • OleDbConnection
  • OdbcConnection
  • FbConnection

ConnectionStrings项包含一个 名称, 但这些都不是名称的对象的后裔类别,但似乎是一个 namespace

我怎么把建造一个对象基础上的一串 名称?


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,可用于建造连接。我没有尝试这样的代码,但我认为它会起作用。这是可能的,你可能需要查找供应商的名称使用GetFactoryClasses方法上的DbProviderFactories类和使用的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;
}

其他提示

检查了 这Hanselman的博客 上添加自定义的建设类型不同的连接串名称,它看起来像它可能适合你想要完成在一个不同的方式使用的供应商类型。

如果名称的特定的连接名(开发、测试、prod)永远不会改变,为什么你不做一个开关在connectionName param你的方法和设置的名称的实例这样的吗?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top