.net C# 通过连接字符串检索正确的 DbConnection 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/185474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:17:04  来源:igfitidea点击:

C# Retrieving correct DbConnection object by connection string

.netado.netconnection-stringdbconnection

提问by johnc

I have a connection string being passed to a function, and I need to create a DbConnection based object (i.e. SQLConnection, OracleConnection, OLEDbConnection etc) based on this string.

我有一个传递给函数的连接字符串,我需要基于这个字符串创建一个基于 DbConnection 的对象(即 SQLConnection、OracleConnection、OLEDbConnection 等)。

Is there any inbuilt functionality to do this, or any 3rd party libraries to assist. We are not necessarily building this connection string, so we cannot rely on a format the string is written in to determine its type, and I would prefernot to have to code up all combinations and permutations of possible connection strings

是否有任何内置功能可以做到这一点,或者任何 3rd 方库可以提供帮助。我们不一定要构建此连接字符串,因此我们不能依赖写入字符串的格式来确定其类型,而且我宁愿不必编码可能连接字符串的所有组合和排列

回答by Mark Cidade

   DbConnection GetConnection(string connStr)
    { string providerName = null;
      var    csb = new DbConnectionStringBuilder{ConnectionString=connStr};

      if (csb.ContainsKey("provider")) 
       { providerName = csb["provider"].ToString();
       }          
      else
       { var css = ConfigurationManager
                           .ConnectionStrings
                           .Cast<ConnectionStringSettings>()
                           .FirstOrDefault(x=>x.ConnectionString==connStr);
         if (css != null) providerName = css.ProviderName;
       }

      if (providerName != null) 
       { var providerExists =  DbProviderFactories
                                     .GetFactoryClasses()
                                     .Rows.Cast<DataRow>()
                                     .Any(r=>r[2].Equals(providerName));
         if (providerExists) 
          { var factory = DbProviderFactories.GetFactory(providerName);
            var dbConnection = factory.CreateConnection();

            dbConnection.ConnectionString = connStr;
            return dbConnection;
          }
       }

      return null;
   }

回答by Eric Tuttleman

if you're using framework 2.0 or above, and you can get them to pass in a second string with the driver class, you can use the dbProviderFactory class to load the driver for you.

如果您使用的是 2.0 或更高版本的框架,并且可以让它们通过驱动程序类传入第二个字符串,则可以使用 dbProviderFactory 类为您加载驱动程序。

DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(myDriverClass);
DbConnection dbConnection = dbProviderFactory.CreateConnection();
dbConnection.ConnectionString = myConnectionString;

Here's an MSDN link to the Factory class: http://msdn.microsoft.com/en-us/library/wda6c36e.aspx

这是工厂类的 MSDN 链接:http: //msdn.microsoft.com/en-us/library/wda6c36e.aspx

回答by Bryant

Most connection strings (at least in .NET 2.0) also have a providerName property that goes with them. So a SQL connection string will have a provider Name like:

大多数连接字符串(至少在 .NET 2.0 中)也有一个与它们一起使用的 providerName 属性。所以一个 SQL 连接字符串将有一个提供者名称,如:

providerName="System.Data.SqlClient"

So your method would need to accept both the connection string and the provider name and then you could use the DbProviderFactory as mentioned by damieng.

所以你的方法需要接受连接字符串和提供者名称,然后你可以使用damieng 提到的 DbProviderFactory

回答by DamienG

You should be able to parse out the Provider section and pass it into DbProviderFactories.GetFactory which will return a OdbcFactory, OleDbFactory or SqlClientFactory and let you then perform CreateConnection etc.

您应该能够解析出 Provider 部分并将其传递给 DbProviderFactories.GetFactory ,它将返回 OdbcFactory、OleDbFactory 或 SqlClientFactory,然后让您执行 CreateConnection 等。

I'm not sure how this would work with Oracle unless they provide an OracleDbFactory.

我不确定这将如何与 Oracle 一起使用,除非他们提供 OracleDbFactory。