【问题标题】:asp.net - adding MARS to connection from web.configasp.net - 将 MARS 添加到来自 web.config 的连接
【发布时间】:2017-01-30 02:26:57
【问题描述】:

我在 webconfig 中有这个

<add name="dbConn" connectionString="Data Source=PC-PC;Integrated Security=True" />

然后我大部分时间在我的页面中通过

调用它们
 string connstr = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;

但是有些页面需要两个查询。我的想法是(或我想要实现的)

 string connstr = ConfigurationManager.ConnectionStrings["dbConn"+"MultipleActiveResultSets=True"].ConnectionString;

但它当然行不通。由于here 中所述的内容类似于下面的代码。

脚注:我不想在我的大部分页面中使用它

string connectionString = "Data Source=MSSQL1;" + 
        "Initial Catalog=AdventureWorks;Integrated Security=SSPI;" +
        "MultipleActiveResultSets=True";

因为我有多个页面,当然要轻松设置数据库。
脚注2:原因是因为我不知道它可能不健壮,并且在只需要 1 个连接的页面中使用 2 个连接可能很难看。
编辑:抱歉英语不好

【问题讨论】:

    标签: c# asp.net sql-server-mars


    【解决方案1】:

    您的场景可以有两个连接:

    没有MultipleActiveResultSets=True,

    <add name="dbConn1" connectionString="Data Source=PC-PC;Integrated Security=True" />
    

    MultipleActiveResultSets=True,

    <add name="dbConn2" connectionString="Data Source=MSSQL1; 
            Initial Catalog=AdventureWorks;Integrated Security=SSPI;
            MultipleActiveResultSets=True;" />
    

    您可以根据需要加载以上连接字符串。

    【讨论】:

    • 您好,我尝试在 web.config 中添加 2 connectionstring,但 ASP 不允许。我真的很希望发生这种情况(只是在启用和无 MARS 连接之间切换连接)但是如果我将 MARS 用于不需要 mars 的页面是否重要
    • AFAIK,web.config 中应该允许多个连接字符串在您的场景中使用 MARS 将是一个问题!!!
    【解决方案2】:

    在SqlConnectionStringBuilder中加载连接字符串,操作builder,然后调用ToString()获取操作后的连接字符串:

    string webConfigConnectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
    var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(webConfigConnectionString);
    builder.MultipleActiveResultSets = true;
    string modifiedConnectionString = builder.ToString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多