首页 > asp.net 的页面为什么会卡在那里?总是遇到这样的问题,排查起来,感觉很无力。

asp.net 的页面为什么会卡在那里?总是遇到这样的问题,排查起来,感觉很无力。

一开始以为是数据库死锁造成的
后来设置了数据库锁超时时间为2000毫秒
但是还是会出现页面一直卡在那里,不知道为什么?

sql server 设置锁超时时间

set lock_timeout 2000
select @@lock_timeout

难道代码逻辑有问题?
C# 代码如下:

[down.aspx]

string idString = Request.QueryString["id"] ?? string.Empty;
int id = 0;
if(!int.TryParse(idString ,out id))
{
    return;
}   

var dbeh = Common.DB.Factory.CreateDBEntityHelper();
if (int.TryParse(idString, out id))
{
    var dlh = new DownLinkHelper();
    string downlink = dlh.GetDownlink(id);

    if(!string.IsNullOrEmpty(downlink))
    {
        Response.Redirect(Server.UrlPathEncode( Server.UrlDecode(downlink)));               
    }

}

[DownLinkHelper.cs]

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

/// <summary>
/// DownLink 的摘要说明
/// </summary>
public class DownLinkHelper
{
    Common.DB.IDBEntityHelper dbh = Common.DB.Factory.CreateDBEntityHelper();

    public string GetDownlink(int val)
    {
        var ent = dbh.GetEntity<Entities.SoftDownLink>("SoftID=" + val);
        if (ent != null)
        {               
            return ent.Address;          
        }

        return string.Empty;
    }
}











using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

/// <summary>
///SQLServerHandler 的摘要说明
/// </summary>
public class DBHelper : IDBHelper
{
    string connectionString;
    public string ConnectionString
    {
        get { return connectionString; }
        set { connectionString = value; }
    }

    public DBHelper(string connectionString)
    {
        this.connectionString = connectionString;
    }

    public DbCommand CreateCommand()
    {
        return new SqlCommand();
    }

    public DbConnection CreateConnection()
    {

        return new SqlConnection(connectionString);
    }


    public DbParameter CreateParameter(string name, object value)
    {
        DbParameter parameter = CreateParameter();
        parameter.ParameterName = name;
        parameter.Value = value;
        return parameter;
    }

    public DbParameter CreateParameter(string name)
    {
        DbParameter parameter = CreateParameter();
        parameter.ParameterName = name;
        return parameter;
    }

    public DbParameter CreateParameter()
    {
        return new SqlParameter();
    }


    public T ExecuteScalar<T>(string sql, params DbParameter[] parameters)
    {

        using (DbConnection connection = CreateConnection())
        {
            DbCommand cmd = CreateCommand();
            cmd.Connection = connection;
            cmd.CommandText = sql;
            cmd.Parameters.AddRange(parameters);
            connection.Open();
            object o = cmd.ExecuteScalar();
            connection.Close();
            return (T)Convert.ChangeType(o, typeof(T));
        }
    }


    public DbDataReader ExecuteReader(string sql, params DbParameter[] parameters)
    {
        DbConnection connection = CreateConnection();

        DbCommand cmd = CreateCommand();
        cmd.Connection = connection;
        cmd.CommandText = sql;
        cmd.Parameters.AddRange(parameters);
        connection.Open();
        return cmd.ExecuteReader(CommandBehavior.CloseConnection);
    }

    public int ExecuteNoneQuery(string sql, params DbParameter[] parameters)
    {
        using (DbConnection connection = CreateConnection())
        {

            DbCommand cmd = CreateCommand();
            cmd.Connection = connection;
            cmd.CommandText = sql;
            cmd.Parameters.AddRange(parameters);
            connection.Open();
            int num = cmd.ExecuteNonQuery();
            connection.Close();
            return num;
        }
    }

    public List<Hashtable> GetDataList(string sql, params DbParameter[] parameters)
    {

        List<Hashtable> list = new List<Hashtable>();
        using (DbDataReader reader = ExecuteReader(sql, parameters))
        {
            while (reader.Read())
            {
                Hashtable hs = new Hashtable();

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    hs[reader.GetName(i)] = reader.GetValue(i);
                }

                list.Add(hs);
            }
            reader.Close();
            reader.Dispose();
        }

        return list;


    }

    public Hashtable GetData(string sql, params DbParameter[] parameters)
    {
        Hashtable hs = null;
        using (DbDataReader reader = ExecuteReader(sql, parameters))
        {
            while (reader.Read())
            {
                hs = new Hashtable();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    hs[reader.GetName(i)] = reader.GetValue(i);
                }
            }
            reader.Close();
            reader.Dispose();
        }

        return hs;
    }



}






/// <summary>
/// DbEntityHelper 的摘要说明
/// </summary>
public class DBEntityHelper : IDBEntityHelper
{
    IDBHelper db = null;

    public IDBHelper DBHelper
    {
        get { return db; }
        set { db = value; }
    }


    public DBEntityHelper()
    {

    }




    public T GetEntityBySql<T>(string sql, params DbParameter[] parameters)
    {
        List<T> list = GetDataList<T>(sql, parameters);

        if (list != null)
        {
            if (list.Count == 1)
            {
                return list[0];
            }
        }

        return default(T);
    }


    public T GetEntity<T>(int id)
    {
        return GetEntityById<T, int>(id);
    }

    public T GetEntity<T, IDT>(IDT id)
    {
        return GetEntityById<T, IDT>(id);
    }

    public T GetEntityById<T, IDT>(IDT id)
    {
        string idFieldName = FieldAttribute.GetIDFieldName(typeof(T));

        if (!string.IsNullOrEmpty(idFieldName))
        {
            return GetEntity<T>(idFieldName + "=@ID", db.CreateParameter("@ID", id));
        }

        return default(T);
    }

    public T GetEntity<T>(string where, params DbParameter[] parameters)
    {
        return GetEntityByWhere<T>(where, parameters);
    }

    public T GetEntity<T>(string where, string sort, params DbParameter[] parameters)
    {
        return GetEntityByWhere<T>(where, sort, parameters);
    }


    public T GetEntityByWhere<T>(string where, params DbParameter[] parameters)
    {
        return GetEntityByWhere<T>(where, null, parameters);
    }

    public T GetEntityByWhere<T>(string where, string sort, params DbParameter[] parameters)
    {
        List<T> list = GetDataList<T>(1, where, sort, parameters);
        if (list != null)
        {
            if (list.Count == 1)
            {
                return list[0];
            }
        }
        return default(T);
    }





    /// <summary>
    /// sql 获得列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="queryString"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public List<T> GetDataList<T>(string queryString, params DbParameter[] parameters)
    {
        queryString = queryString.Trim();
        PropertyInfo[] propertys = typeof(T).GetProperties();
        string sql = null;
        if (queryString.IndexOf("select", StringComparison.OrdinalIgnoreCase) == 0)
        {
            sql = queryString;
        }
        else
        {
            Type typ = typeof(T);

            string tableName = TableAttribute.GetTableName(typ);



            StringBuilder fields = new StringBuilder("0");
            foreach (PropertyInfo pi in propertys)
            {
                fields.Append(",");

                string fsql = FieldAttribute.GetFieldSQL(pi);
                if (!string.IsNullOrEmpty(fsql))
                {
                    fields.Append(fsql);
                }
                else
                {
                    fields.Append(FieldAttribute.GetFieldName(pi));
                }



            }

            if (queryString.IndexOf("where", StringComparison.OrdinalIgnoreCase) == 0)
            {
                sql = "select top 1000 " + fields.ToString() + " from " + tableName + " with(nolock) " + queryString;
            }
            else
            {
                sql = "select top 1000 " + fields.ToString() + " from " + tableName + " with(nolock) where " + queryString;
            }


        }

        List<T> list = new List<T>(30);



        using (DbDataReader reader = db.ExecuteReader(sql, parameters))
        {
            while (reader.Read())
            {
                T ins = Activator.CreateInstance<T>();

                foreach (PropertyInfo pi in propertys)
                {
                    int inx = reader.GetOrdinal(FieldAttribute.GetFieldName(pi).Replace("[", string.Empty).Replace("]", string.Empty));

                    object value = reader.GetValue(inx);

                    if (value == DBNull.Value)
                    {
                        pi.SetValue(ins, null, null);
                    }
                    else
                    {

                        pi.SetValue(ins, value, null);
                    }
                }

                list.Add(ins);
            }
            reader.Close();
            reader.Dispose();
        }





        return list;
    }




    /// <summary>
    /// 获得列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="num">选择多少条,如果num &lt;= 0 则选择全部</param>
    /// <param name="where"></param>
    /// <param name="sort"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public List<T> GetDataList<T>(int num, string where, string sort, params DbParameter[] parameters)
    {
        Type typ = typeof(T);

        string tableName = TableAttribute.GetTableName(typ);

        PropertyInfo[] propertys = typ.GetProperties();

        StringBuilder fields = new StringBuilder("0");
        foreach (PropertyInfo pi in propertys)
        {
            fields.Append(",");

            string fsql = FieldAttribute.GetFieldSQL(pi);
            if (!string.IsNullOrEmpty(fsql))
            {
                fields.Append(fsql);
            }
            else
            {
                fields.Append(FieldAttribute.GetFieldName(pi));
            }




        }

        where = where ?? string.Empty;
        if (!string.IsNullOrEmpty(where))
        {
            where = " where " + where;
        }

        sort = sort ?? string.Empty;
        if (!string.IsNullOrEmpty(sort))
        {
            sort = " order by " + sort;
        }

        string sql = null;
        if (num > 0)
        {
            sql = "select top " + num + " " + fields.ToString() + " from " + tableName + " with(nolock) " + where + " " + sort;
        }
        else
        {
            sql = "select " + fields.ToString() + " from " + tableName + "  with(nolock) " + where + " " + sort;

        }

        return GetDataList<T>(sql, parameters);
    }


    /// <summary>
    /// 选择全部数据列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public List<T> GetDataList<T>()
    {

        return GetDataList<T>(100, null, null);
    }



    /// <summary>
    /// 获得一个分页数据列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="pageNumber"></param>
    /// <param name="pageSize"></param>
    /// <param name="where"></param>
    /// <param name="sort"></param>
    /// <param name="recordCount"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    public List<T> GetPageDataList<T>(int pageNumber, int pageSize, string where, string sort, out int recordCount, params DbParameter[] parameters)
    {
        Type typ = typeof(T);

        string tableName = TableAttribute.GetTableName(typ);

        PropertyInfo[] propertys = typ.GetProperties();

        StringBuilder fields = new StringBuilder("0");
        string identity = string.Empty;
        foreach (PropertyInfo pi in propertys)
        {
            if (FieldAttribute.IsIdentity(pi)) identity = FieldAttribute.GetFieldName(pi);

            fields.Append(",");

            string fsql = FieldAttribute.GetFieldSQL(pi);
            if (!string.IsNullOrEmpty(fsql))
            {
                fields.Append(fsql);
            }
            else
            {
                fields.Append(FieldAttribute.GetFieldName(pi));
            }


            //fields.Append(FieldAttribute.GetFieldName(pi));

        }

        QueryStringHelper query = new QueryStringHelper();
        query.Fields = fields.ToString();
        query.Identity = identity;
        query.Table = tableName;
        query.PageSize = pageSize;
        query.AbsolutePage = pageNumber;
        query.Where = where;
        query.Sort = sort;


        DbParameter[] cps = new DbParameter[parameters.Length];
        for (int i = 0; i < parameters.Length; i++)
        {
            DbParameter para = parameters[i];

            cps[i] = db.CreateParameter(para.ParameterName, para.Value);
        }


        recordCount = db.ExecuteScalar<int>(query.GetCountQueryString(), cps);

        string selectString = query.GetQueryString();

        return GetDataList<T>(selectString, parameters);
    }

    /// <summary>
    /// 修改数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool UpdateData<T>(T entity)
    {
        Type typ = typeof(T);
        PropertyInfo[] propertys = typeof(T).GetProperties();
        string tableName = TableAttribute.GetTableName(typ);

        List<DbParameter> list = new List<DbParameter>(propertys.Length);


        StringBuilder fps = new StringBuilder();

        bool first = true;

        string where = string.Empty;
        foreach (PropertyInfo pi in propertys)
        {
            if (FieldAttribute.IsAllowModifyUndefined(pi))
            {
                continue;
            }

            if (!FieldAttribute.IsIdentity(pi))
            {
                Type pp = pi.PropertyType;

                object value = pi.GetValue(entity, null);
                if (pp.IsValueType)
                {
                    if (value == null)
                    {
                        continue;
                    }
                }


                if (!first)
                {
                    fps.Append(",");
                }
                else
                {
                    first = false;
                }


                fps.Append(FieldAttribute.GetFieldName(pi));
                fps.Append("=@");
                fps.Append(pi.Name);


                list.Add(db.CreateParameter("@" + pi.Name, value));
            }
            else
            {
                where = FieldAttribute.GetFieldName(pi) + "=@" + pi.Name;
                list.Add(db.CreateParameter("@" + pi.Name, pi.GetValue(entity, null)));
            }
        }


        int num = db.ExecuteNoneQuery("update " + tableName + " set " + fps + " where " + where, list.ToArray());
        if (num == 1)
        {
            return true;
        }

        return false;
    }



    /// <summary>
    /// 插入数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool InsertData<T>(T entity)
    {

        Type typ = typeof(T);
        PropertyInfo[] propertys = typeof(T).GetProperties();
        string tableName = TableAttribute.GetTableName(typ);

        List<DbParameter> list = new List<DbParameter>(propertys.Length);


        StringBuilder fields = new StringBuilder();
        StringBuilder parames = new StringBuilder();

        bool first = true;

        foreach (PropertyInfo pi in propertys)
        {
            if (!FieldAttribute.IsIdentity(pi))
            {
                Type pp = pi.PropertyType;
                object value = pi.GetValue(entity, null);
                if (pp.IsValueType)
                {
                    if (value == null)
                    {
                        continue;
                    }
                }

                if (!first)
                {
                    fields.Append(",");
                    parames.Append(",");
                }
                else
                {
                    first = false;
                }


                fields.Append(FieldAttribute.GetFieldName(pi));


                parames.Append("@");
                parames.Append(pi.Name);

                list.Add(db.CreateParameter("@" + pi.Name, value));
            }
        }



        int num = db.ExecuteNoneQuery("insert into " + tableName + "(" + fields + ") values(" + parames + ")", list.ToArray());
        if (num == 1)
        {
            return true;
        }

        return false;
    }

    /// <summary>
    /// 插入数据,并返回最终插入的ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="RT"></typeparam>
    /// <param name="entity"></param>
    /// <returns></returns>
    public RT InsertDataAndGetID<T, RT>(T entity)
    {
        Type typ = typeof(T);
        PropertyInfo[] propertys = typeof(T).GetProperties();
        string tableName = TableAttribute.GetTableName(typ);

        List<DbParameter> list = new List<DbParameter>(propertys.Length);


        StringBuilder fields = new StringBuilder();
        StringBuilder parames = new StringBuilder();

        bool first = true;

        foreach (PropertyInfo pi in propertys)
        {
            if (!FieldAttribute.IsIdentity(pi))
            {
                Type pp = pi.PropertyType;
                object value = pi.GetValue(entity, null);
                if (pp.IsValueType)
                {
                    if (value == null)
                    {
                        continue;
                    }
                }

                if (!first)
                {
                    fields.Append(",");
                    parames.Append(",");
                }
                else
                {
                    first = false;
                }


                fields.Append(FieldAttribute.GetFieldName(pi));


                parames.Append("@");
                parames.Append(pi.Name);

                list.Add(db.CreateParameter("@" + pi.Name, value));
            }
        }


        string sql = "insert into " + tableName + "(" + fields + ") values(" + parames + ");select @@identity;";

        return db.ExecuteScalar<RT>(sql, list.ToArray());
    }


}

先确认是后端无响应还是前端浏览器挂了。
后端无响应时,在调试模式下一定会有异常发生,所以观察异常就行,
如果是前端渲染或者脚本导致卡死的。那么就需要一步一步检查代码了。


var dbeh = Common.DB.Factory.CreateDBEntityHelper();

这句有什么作用?

【热门文章】
【热门文章】