質問

いまの構築DbConnectionに基づく プロバイダ名?

サンプル プロバイダ名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"/>

ご覧のと異なる提供者にあります。その時私にとって接続を作成し、そのDbConnectionを、例えば:

  • SqlConnection
  • OleDbConnection
  • OdbcConnection
  • FbConnection

のconnectionStrings作品が含まれて providerName, でんの名前をDbConnection子孫の授業では見 名前空間

いく構築DbConnectionに基づく文字列 providerName?


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();

}
役に立ちましたか?

解決

あなたはこのルートを行けば、私はあなたが接続を構築するために使用することができますDbProviderFactoryを取得するにはDbProviderFactoriesクラスを使用したいと思います。私はこのコードを試していないが、私はそれが動作すると思います。それはあなたが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;
}

他のヒント

異なる接続文字列名のカスタムビルドの種類を追加するには、このHanselman氏のブログをチェック、それはそれはあなたがプロバイダーの種類を使用するよりも別の方法で達成したいものを合うことのように見えます。

特定の接続名に対してProviderName(DEV、テスト、PRODは)なぜあなたはあなたの方法のためにはconnectionNameのPARAMのスイッチを行うとproviderNameでインスタンスをそのように設定しカント変わることはありませんか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top