.NET SQL连接代码阻止所有其他连接

时间:2014-03-13 21:19:23

标签: c# sql .net sql-server

如果主题没有准确反映我的确切问题,我很难道歉,我很难解释我遇到的问题,虽然看起来很简单。

我已经构建了一个简单的“db helper”类,它为我执行sql语句,给出一些参数等。这是代码块:

public DataSet selectSprocData(string sprocName, SqlParameter[] parameterArray, out int returnValue)
    {

        //processes the specified Select stored procedure based on parameter array provided;
        //this is the only place anywhere in the application we will do a simple SELECT using a sproc.

        DataSet dataset = new DataSet();

        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ToString()))
        {
            cn.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(sprocName, cn);
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.Parameters.AddRange(parameterArray);
            SqlParameter retValParam = adapter.SelectCommand.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            retValParam.Direction = ParameterDirection.ReturnValue;
            adapter.SelectCommand.CommandTimeout = 600;
            adapter.Fill(dataset);
            returnValue = (int)retValParam.Value;
            adapter.Dispose();
            cn.Close();
        }

        return dataset;
    }

当我使用长时间运行的sproc并在SSMS中执行它时它会运行并最终超时。同时我可以在SSMS中打开另一个查询窗口,并对我的数据库执行任何其他选择或查询。

现在,当我使用上面的代码块通过我的web-app调用此sproc时,页面将旋转并加载并加载,直到最终(几分钟后)该进程将超时。

但是,在这个基于Web的调用期间,我无法打开任何其他窗口并执行任何其他使用相同db代码调用其他sprocs的UI函数。

基本上,一个用户从UI执行一个sproc / function需要很长时间,似乎阻止其他人在我的应用程序上做任何事情。

据我所知,首先我需要有更好的查询,这些查询不会超时,但是有些东西我在.net / c#中缺少或不能正确地执行所有其他连接或命令尝试被阻止,直到另一个完成或超时?

我的web.config connectionstring没有特殊参数,只需:

Persist Security Info=False;User ID=sa;Password=xxxx;Initial Catalog=db_live;Data Source=my.host.com"

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您无法将Windows应用程序(SQL Management Studio)与ASP.NET Web应用程序(在IIS中运行)进行比较

您的ASP.NET代码的ALl在单个线程上运行,因此该线程必须等待数据库代码完成,这将阻止所有其他内容,直到完成为止。

使用ASP.NET Update面板执行长时间运行的存储过程和UI绑定。

ASP.NET Update Panel

相关问题