C#中通过连接字符串获取正确的DbConnection对象 [英] C# Retrieving correct DbConnection object by connection string

查看:1512
本文介绍了C#中通过连接字符串获取正确的DbConnection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接字符串被传递给函数,我需要在此基础上字符串,创建的DbConnection基于对象(即的SQLConnection,为OracleConnection,OleDbConnection的等等)。

有没有什么内在的功能来做到这一点,或任何第三方库,以协助。我们不一定建设这个连接字符串,所以我们不能依赖于字符串写入,以确定其类型的格式,我会的 preFER 的不要有code所有组合和可能的连接字符串的排列

解决方案

 的DbConnection的getConnection(字符串connStr)
    {字符串的providerName = NULL;
      VAR CSB =新DbConnectionStringBuilder {的ConnectionString = connStr};

      如果(csb.ContainsKey(提供者))
       {的providerName = CSB [提供者]的ToString();
       }
      其他
       {VAR CSS = ConfigurationManager中
                           .ConnectionStrings
                           .Cast< ConnectionStringSettings>()
                           .FirstOrDefault(X => x.ConnectionString == connStr);
         如果(CSS!= NULL)的providerName = css.ProviderName;
       }

      如果(的providerName!= NULL)
       {VAR providerExists = DbProviderFactories
                                     .GetFactoryClasses()
                                     .Rows.Cast< D​​ataRow的>()
                                     。任何相关(r => R [2] .Equals(的providerName));
         如果(providerExists)
          {VAR厂= DbProviderFactories.GetFactory(的providerName);
            VAR的DbConnection = factory.CreateConnection();

            dbConnection.ConnectionString = connStr;
            返回的DbConnection;
          }
       }

      返回null;
   }
 

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.

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 prefer not to have to code up all combinations and permutations of possible connection strings

解决方案

   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;
   }

这篇关于C#中通过连接字符串获取正确的DbConnection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆