不支持关键字 MultipleActiveResultSets

发布于 2024-08-22 22:46:37 字数 1675 浏览 6 评论 0原文

我们在 ASP.NET MVC 应用程序中使用自定义成员资格提供程序。我们一直遇到间歇性的“System.InvalidOperationException - 已经有一个与此命令关联的打开的 DataReader,必须先将其关闭。”课程出现问题,所以我决定启用 MARS。

这是 Web 配置中的连接字符串...

<add name="CustomMembershipServices" connectionString="User ID=CUSTOM_USER;Password=pwd;Database=OUR_DB;Server=.\SQLEXPRESS;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />

我们的代码现在在下一行抛出 ArgumentException: Keyword not support: 'multipleactiverecordsets'。

protected void CreateAndOpenConnection()
{
    // Exception thrown here...
    _connection = new SqlConnection(_connectionString);
    _connection.Open();
}

针对 SQL2005 Express 和 SQL2008 Standard 版本时会出现相同的异常。有什么建议吗?

为了响应 AdaTheDev,这里是使用数据读取器的方法...

public bool CheckUserPassword(long userID, string password)
{
    bool success = false;

    if (!string.IsNullOrEmpty(password))
    {
        try
        {
            CreateAndOpenConnection();

            using (SqlCommand cmd = CreateSqlCommandForStoredProcedure("CheckPassword"))
            {
                IPasswordUtility generator = new PasswordUtility();

                cmd.Parameters.AddWithValue("UserID", userID);
                cmd.Parameters.AddWithValue("Password", generator.HashPassword(password));

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    success = reader.HasRows;

                    reader.Close();
                }
            }
        }
        finally
        {
            CloseConnection();
        }
    }

    return success;
}

我看不出为什么这会在同一连接上创建多个数据读取器的任何原因。

We are using a custom Membership Provider in an ASP.NET MVC application. We have been experiencing intermittent 'System.InvalidOperationException - There is already an open DataReader associated with this Command which must be closed first.' problems with the class so I decided to enabled MARS.

This is the connection string in the web config...

<add name="CustomMembershipServices" connectionString="User ID=CUSTOM_USER;Password=pwd;Database=OUR_DB;Server=.\SQLEXPRESS;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />

Our code now throws an ArgumentException: Keyword not supported: 'multipleactiverecordsets' at the following line..

protected void CreateAndOpenConnection()
{
    // Exception thrown here...
    _connection = new SqlConnection(_connectionString);
    _connection.Open();
}

The same exception is seen when targeting SQL2005 Express and SQL2008 Standard editions. Any suggestions?

In response to AdaTheDev here is the method that uses the data reader...

public bool CheckUserPassword(long userID, string password)
{
    bool success = false;

    if (!string.IsNullOrEmpty(password))
    {
        try
        {
            CreateAndOpenConnection();

            using (SqlCommand cmd = CreateSqlCommandForStoredProcedure("CheckPassword"))
            {
                IPasswordUtility generator = new PasswordUtility();

                cmd.Parameters.AddWithValue("UserID", userID);
                cmd.Parameters.AddWithValue("Password", generator.HashPassword(password));

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    success = reader.HasRows;

                    reader.Close();
                }
            }
        }
        finally
        {
            CloseConnection();
        }
    }

    return success;
}

I cannot see any reason why this would be creating multiple data readers on the same connection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-08-29 22:46:37

除非您确实出于功能原因需要 MARS,否则我的建议是关注根本问题 - 听起来(我可能是错的!)该类的预期/预期行为是不要针对单个连接打开多个数据读取器同时。

如果是这种情况,那么您应该确保在同一连接上打开另一个数据读取器之前正确关闭数据读取器 - 听起来好像存在未完成此操作的情况。使用 MARS 作为解决方法仍然会留下某些内容未正确关闭的事实。

编辑1:
在清理数据读取器等方面,该代码对我来说看起来确实不错。除非正在进行一些多线程处理,通过使用共享 _connection 对象导致问题? (我怀疑情况是否如此)。类中是否有其他方法不会关闭数据读取器?

Unless you actually need MARS for a functional reason, my recommendation would be to focus on the underlying issue - it sounds like (and I could be wrong!) the expected/intended behaviour of the class is to not open multiple datareaders against a single connection at the same time.

If that is the case, then you should make sure datareaders are closed properly before another is opened on the same connection - it sounds like there is a situation where this is not being done. Using MARS as a workaround would still leave the fact that there is something not being closed properly.

Edit 1:
That code does look OK to me in terms of clearing up the datareaders etc. Unless there's some multi-threading going on, causing a problem through the use of the shared _connection object? (I'm doubting this is the case). Are there any other methods in the class that aren't closing a datareader?

一紙繁鸢 2024-08-29 22:46:37

问题是由于 ASP.NET 提供程序框架将我的类视为静态单例...

"问题:非静态类什么时候是静态的?"

The problem is due to the ASP.NET Provider framework treating my class as a static singleton ...

"Question: When is a non-static class static?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文