如何启用MultipleActiveResultSets

时间:2013-03-31 18:00:46

标签: c# asp.net sql

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

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

我的webconfig看起来像这样:

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

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

3 个答案:

答案 0 :(得分:39)

这很简单,只需添加

MultipleActiveResultSets=true;

以这种方式在web.config中更改连接字符串:

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

答案 1 :(得分:4)

试试此代码

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

必须参考此Msdn article

答案 2 :(得分:0)

 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;
            }

        }
    }

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