超时已过期。操作完成之前经过的超时时间或服务器没有响应。“

时间:2015-09-24 05:04:26

标签: c# asp.net stored-procedures

我正在运行存储过程,但在运行时显示错误,如

  

超时已过期。操作完成之前经过的超时时间或服务器没有响应。

我已将我的网络配置设置为

   <add name="SAMPLECONNECTION" connectionString="Data Source=ADMIN\SQLEXPRESS;Initial Catalog=PFGRT;Persist Security Info=True;MultipleActiveResultSets=true;User ID=sa;connect Timeout =1000000;Password=PASSWORD"
  providerName="System.Data.SqlClient" />

我尝试设置connect Timeout = 0;,但我得到了同样的错误。

我该如何解决?

我的存储过程调用类

public DataSet adFeedBackReportFacultyWise(string SetID, string FacultyID, string SubjectID,string groupid)
{
    DbUtil du = new DbUtil();
    Dictionary<string, SqlParameter> cmdParameters = new Dictionary<string, SqlParameter>();
    cmdParameters["SetID"] = new SqlParameter("SetID", SetID);
    cmdParameters["FacultyID"] = new SqlParameter("FacultyID", FacultyID);
    cmdParameters["SubjectID"] = new SqlParameter("SubjectID", SubjectID);
    cmdParameters["groupid"] = new SqlParameter("groupid", groupid);

    return du.ExecuteQuery("adFeedBackReportFacultyWise", cmdParameters);
}

我的DButil类是

public class DbUtil
{
    string connection = ConfigurationManager.ConnectionStrings["gatikkcmsConnectionString"].ConnectionString;

    public SqlDataAdapter ad;
    public DataTable dt = new DataTable();
    SqlConnection conn;
    SqlCommand cmd = new SqlCommand();
    public DbUtil()
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["gatikkcmsConnectionString"].ConnectionString);
        cmd = null;
    }

    public SqlConnection getConnection()
    {
        return new SqlConnection(connection);
    }
   public int ExecuteCommand(string storedProcName, Dictionary<string, SqlParameter> procParameter)
    {
        int rc;
        using (SqlConnection cn = getConnection())
        {
            cn.Open();
            // create a SQL command to execute the stored procedure
            using (SqlCommand cmd = cn.CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = storedProcName;
                // assign parameters passed in to the command
                foreach (var procParameters in procParameter)
                {
                    cmd.Parameters.Add(procParameters.Value);
                }
                rc = cmd.ExecuteNonQuery();
            }
            cn.Close();
        }
        return rc;
    }
}

2 个答案:

答案 0 :(得分:4)

问题不是连接超时(建立与数据库服务器的连接所花费的时间),而是命令超时(执行命令所用的时间)

DbUtil类中,将命令超时设置为SqlCommand,如下所示:

using (SqlCommand cmd = cn.CreateCommand())
{
    cmd.CommandTimeout = 120; //set timeout - use appropriate value
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storedProcName;
    // assign parameters passed in to the command
    foreach (var procParameters in procParameter)
    {
        cmd.Parameters.Add(procParameters.Value);
    }
    rc = cmd.ExecuteNonQuery();
}

同样值得检查存储过程的性能。

答案 1 :(得分:0)

当你面临命令时,你需要增加sql command的时间。您可以在命令中指定它,如

// Setting command timeout to 2 minutes
cmd.CommandTimeout = 120;