ASP.NET:如何创建一个web.config中的ConnectionString连接? [英] ASP.NET: How to create a connection from a web.config ConnectionString?

查看:1099
本文介绍了ASP.NET:如何创建一个web.config中的ConnectionString连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何构建基于一个供应商名称的DbConnection

样品的提供程序名称取值

  • System.Data.SqlClient的
  • System.Data.OleDb
  • System.Data.Odbc
  • FirebirdSql.Data.FirebirdClient

我已经保存在我的IIS服务器的web.config文件中的连接字符串:

 <的ConnectionStrings>
  <添加名称=发展
        的connectionString =供应商= IBMDA400;数据源= MY_SYSTEM_NAME;用户ID =名为myUsername;密码= MYPASSWORD;
        的providerName =System.Data.OleDb/>
  <添加名称=活
        的connectionString =美元= SA; PWD =密码;服务器= deathstar;
        的providerName =System.Data.Odbc/>
  <添加名称=测试
        的connectionString =美元= SA; PWD =密码;服务器= deathstar;
        的providerName =System.Data.SqlClient的/>
  <添加名称=离线
        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


 公开的DbConnection的getConnection(字符串的connectionName)
{
    //获取的connectionString信息来源
    ConnectionStringSettings CS =
          ConfigurationManager.ConnectionStrings [connectionName中];
    如果(CS == NULL)
       抛出新ConfigurationException的(无效的连接名称\+的connectionName +\);

    //根据提供者创建连接
    康涅狄格州的DbConnection =新的DbConnection();

}
 

解决方案

如果你走这条路,我想你会想使用DbProviderFactories类来获得,你可以用它来构建连接DbProviderFactory。我没有试过code,但我认为它会工作。这是可能的,你可能需要查找提供者的名称使用上的DbProviderFactories类GetFactoryClasses方法和使用InvariantName。

 公开的DbConnection的getConnection(字符串的connectionName)
{
   //从web.config中的连接字符串信息
   ConnectionStringSettings CS =
         ConfigurationManager.ConnectionStrings [connectionName中];

   //记录返回空值,如果它不能找到
   如果(CS == NULL)
      抛出新ConfigurationErrorsException(无效的连接名称\+的connectionName +\);

   //获取工厂给定的提供者(如System.Data.SqlClient的)
   DbProviderFactory厂=
         DbProviderFactories.GetFactory(cs.ProviderName);

   //如果GetFactory找不到提供商不确定的行为。
   //防御测试空厂反正
   如果(厂== NULL)
      抛出新的异常(无法对供应商处获得的工厂\+ cs.ProviderName +\);

   //有工厂给我们正确的连接对象
   康涅狄格州的DbConnection = factory.CreateConnection();

   //如果创建连接失败未定义行为
   //防御测试空连接反正
   如果(康涅狄格州== NULL)
      抛出新的异常(无法获得厂家的连接);

   //知道了连接字符串,打开连接
   conn.ConnectionString = cs.ConnectionString;
   conn.Open()

   返回康涅狄格州;
}
 

How do you construct a DbConnection based on a provider name?

Sample provider names

  • System.Data.SqlClient
  • System.Data.OleDb
  • System.Data.Odbc
  • FirebirdSql.Data.FirebirdClient

i have connection strings stored in my IIS server's web.config file:

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

You can see they all use different providers. When it comes time for me to create a connection, i have to know what kind of DbConnection to create, e.g.:

  • SqlConnection
  • OleDbConnection
  • OdbcConnection
  • FbConnection

The connectionStrings entries contains a providerName, but these aren't the names of DbConnection descendant classes, but appear to be a namespace

How do i turn construct a DbConnection based on a string 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();

}

解决方案

If you go this route, I think you'll want to use the DbProviderFactories class to get a DbProviderFactory that you can use to construct the connection. I haven't tried this code out, but I think it will work. It's possible that you may need to look up the provider name using the GetFactoryClasses method on the DbProviderFactories class and use the 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;
}

这篇关于ASP.NET:如何创建一个web.config中的ConnectionString连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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