C# 如何启用 MultipleActiveResultSets

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15732642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:46:55  来源:igfitidea点击:

How to enable MultipleActiveResultSets

c#asp.netsql

提问by HymanofAll

I have the following connection string in my code:

我的代码中有以下连接字符串:

SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);

My webconfig for this looks like this:

我的网络配置如下所示:

    <connectionStrings>
<add name="RaiseFantasyLeagueConnectionString" connectionString="Data Source=MATT-PC\SQLEXPRESS;Initial Catalog=Raise;Integrated Security=True" providerName="System.Data.SqlClient"/>

Can somebody tell me where I can enable MultipleActiveResultSets for my connection?

有人能告诉我在哪里可以为我的连接启用 MultipleActiveResultSets 吗?

采纳答案by Steve

It is really simple, just add

真的很简单,只需添加

MultipleActiveResultSets=true;

so change, in your web.config, the connection string in this way:

因此,在您的 web.config 中以这种方式更改连接字符串:

connectionString="Data Source=MATT-PC\SQLEXPRESS;" + 
                 "Initial Catalog=Raise;Integrated Security=True;" + 
                 "MultipleActiveResultSets=true;" 

回答by Ramesh Rajendran

Try this code

试试这个代码

<connectionStrings>
<add name="RaiseFantasyLeagueConnectionString" connectionString="Data Source=MATT-PC\SQLEXPRESS;Initial Catalog=Raise;Integrated Security=True ;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient";/>

Must refer this Msdn article

必须参考这篇msdn文章

回答by Developer

 public static class ConfigurationService
    {
        static public string ConnectionString
        {

            get
            {

                try
                {               
                    // Specify the provider name, server and database.
                    string providerName = "System.Data.SqlClient";
                    string serverName = @"192.168.1.106\SQLEXPRESS";
                    string databaseName = "MyDatabaseName";

                    // Initialize the connection string builder for the
                    // underlying provider.
                    var sqlBuilder = new SqlConnectionStringBuilder();

                    // Set the properties for the data source.
                    sqlBuilder.DataSource = serverName;
                    sqlBuilder.InitialCatalog = databaseName;
                    sqlBuilder.IntegratedSecurity = false;
                    sqlBuilder.UserID = "Bob";
                    sqlBuilder.Password = "Bob1234";
                    sqlBuilder.MultipleActiveResultSets = true;
                    sqlBuilder.ApplicationName = "EntityFramework";

                    // Build the SqlConnection connection string.
                    string providerString = sqlBuilder.ToString();

                    // Initialize the EntityConnectionStringBuilder.
                    var entityBuilder = new EntityConnectionStringBuilder();

                    //Set the provider name.
                    entityBuilder.Provider = providerName;

                    // Set the provider-specific connection string.
                    entityBuilder.ProviderConnectionString = providerString;                 

                    // Set the Metadata location.
                    entityBuilder.Metadata = @"res://*/Models.MyDatabaseNameModel.csdl|res://*/Models.MyDatabaseNameModel.ssdl|res://*/Models.MyDatabaseNameModel.msl";

                    var result = entityBuilder.ToString();
                    return result;
                }
                catch (Exception)
                {

                }

                return string.Empty;
            }

        }
    }

Please note that Modelsin Models.MyDatabaseNameModelis a Folder name of your VS project.

请注意,模型Models.MyDatabaseNameModel是你的VS项目的文件夹名称。