ASP.NET:如何从 web.config ConnectionString 创建连接?

2023-11-27

如何构建基于 DbConnection提供商名称?

Sample 提供商名称s

  • 系统.数据.SqlClient
  • 系统.数据.OleDb
  • 系统数据.ODBC
  • FirebirdSql.Data.FirebirdClient

我的 IIS 服务器的 web.config 文件中存储有连接字符串:

<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,例如:

  • sql连接
  • OleDb连接
  • Odbc连接
  • Fb连接

连接字符串条目包含一个提供商名称,但这些不是 DbConnection 后代类的名称,但似乎是名称空间

我如何根据字符串构造 DbConnection提供商名称?


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。我还没有尝试过这段代码,但我认为它会起作用。您可能需要使用 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET:如何从 web.config ConnectionString 创建连接? 的相关文章

随机推荐

  • XAMLParseException 让我抓狂!

    这个 XAMLParseException 让我抓狂 我在 Visual Studio 1020 中使用 NET 3 5 这是我的 xaml
  • 准备从 Python 2.x 转换到 3.x

    到目前为止 我们都知道 我希望如此 Python 3 正在慢慢开始取代 Python 2 x 当然 大多数现有代码最终移植还需要很多年的时间 但我们现在可以在 2 x 版本代码中做一些事情 以使切换更容易 显然是在看什么是新的3 x 中的版
  • 让 will_paginate 与 Ajax 配合使用的最佳方法

    如果你谷歌 will paginate 和 ajax 最上面的结果是这篇博文 但是 will paginate 的原作者说不使用这个方法 对 SEO 蜘蛛不利 但我无法让原作者的方法起作用 他的 javascript 杀死了我所有的链接 另
  • Datetime.ToString() C# 未按预期工作

    From msdn看来我可以创建自己的格式Datetime ToString 方法通过使用M m d y等等 但是当我尝试一个时 它没有按预期工作 下面的截图就是问题所在 我正期待着7 29 2015但收到了7 29 2015 为什么 看起
  • PHP:file_exists 与stream_resolve_include_path - 哪个性能更好?

    似乎最近 php 开发人员对使用它是否更好感到好奇文件已存在 or Stream resolve include path 检查文件是否存在时 无论是包含它们 缓存系统等 这让我想知道是否有人做过基准测试 以判断哪一个是更好的选择 无论是页
  • 如何创建异构对象集合?

    我想在 a 中使用特征对象Vec 在 C 中我可以创建一个基类Thing从中派生出Monster1 and Monster2 然后我可以创建一个std vector
  • 无法对没有 data_class 的表单使用回调断言

    我正在创建一个名为 IntervalType 的自定义 FormType 我的 IntervalType 将有两个字段 start and end并且将是整数类型 此自定义 FormType 将始终在不使用的情况下使用data class
  • 将代理与适用于 Java 的 Google HTTP 客户端库结合使用

    我使用 Google HTTP Client Library for Java 发出简单的 JSON 请求并解析响应 当我不通过代理时它效果很好 但现在我想允许我的用户在我的应用程序中使用代理 带有身份验证 功能 我查看了 HttpTran
  • php 5.4 发生字符串偏移量转换

    在以下几行中 我收到错误 发生字符串偏移转换 code value dictionaryAlias value dictionaryText codeLang code value dictionaryAlias value diction
  • SonataAdminBundle 中的自定义操作

    On this page我找到了如何为我的自定义操作添加路线 protected function configureRoutes RouteCollection collection collection gt add ispremium
  • 您可以拥有的 UIApplicationShortcutItem 数量是否有限制?

    我想将新的 3D Force Touch 功能 UIApplicationShortcutItem 添加到我的应用程序中 但我想知道单个应用程序上可以拥有的数量是否有限制 我查看了文档 但没有看到提到限制 https developer a
  • 禁用焦点上的橙色轮廓突出显示

    我正在使用 jQuery jqTouch 和 Phonegap 编写一个应用程序 并且遇到了一个持续存在的问题 当用户使用软键盘上的 Go 按钮提交表单时 就会出现这个问题 尽管通过使用很容易将光标移动到适当的表单输入元素 input el
  • 即使操作系统杀死应用程序,如何在android中保存/恢复全局变量

    在android中 Google建议我们将全局变量保存在Application中 但有一个问题 如果android操作系统因为内存不足而杀死应用程序 应用程序将重新创建 并且我保存的全局变量将丢失 我不知道什么时候保存 恢复这些变量 应用程
  • 如何计算asp.net中数据表列的总和?

    我有一个有 5 列的 DataTable ID Name 帐号 Branch Amount 数据表包含 5 行 如何将标签控件中金额列的总和显示为 总金额 要计算 DataTable 中列的总和 请使用数据表 计算 method 链接的 M
  • 如何在 appsettings.json 中加载多态对象

    有什么方法可以从中读取多态对象appsettings json以强类型的方式 下面是我需要的一个非常简单的示例 我有多个应用程序组件 名为Features这里 这些组件是由工厂在运行时创建的 我的设计意图是每个组件都由其单独的强类型选项配置
  • sqlite3 中的批量插入速度更快?

    我有一个大约 30000 行数据的文件 我想将其加载到 sqlite3 数据库中 有没有比为每行数据生成插入语句更快的方法 数据以空格分隔并直接映射到 sqlite3 表 是否有任何类型的批量插入方法可以将卷数据添加到数据库中 如果不是内置
  • 如何在远程端存储库上“git Blame”?

    在我的服务器上 我托管我的个人 git 远程项目 使用 gitosis 并且我构建了一个 Web 界面来浏览存储库 类似于 Github 在远程端 您不允许做很多事情 因为缺少工作树 这是正确的 顺便说一句 对于存储库资源管理器 只需很少的
  • 如何更改 WAMPSERVER 中的 MySQL 排序规则

    我如何将 WAMPSERVER 中的 MySQL 排序规则从 latin1 swedish ci 更改为 UTF 8 因为我认为我的 HTML 特殊字符变得一团糟 放入你的C wamp bin mysql mysql5 5 24 my in
  • ListView setOnItemClickListener 在自定义列表视图中不起作用

    我有一个列表视图 每行有两个文本视图和一个编辑文本 列表视图 setOnItemClickListener 不起作用 这是我的 Java 代码 public class CreateChallan extends Activity List
  • ASP.NET:如何从 web.config ConnectionString 创建连接?

    如何构建基于 DbConnection提供商名称 Sample 提供商名称s 系统 数据 SqlClient 系统 数据 OleDb 系统数据 ODBC FirebirdSql Data FirebirdClient 我的 IIS 服务器的