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

一开始以为是数据库死锁造成的
后来设置了数据库锁超时时间为2000毫秒
但是还是会出现页面一直卡在那里,不知道为什么?
sqlserver设置锁超时时间
setlock_timeout2000
select@@lock_timeout
难道代码逻辑有问题?
C#代码如下:
[down.aspx]
stringidString=Request.QueryString["id"]??string.Empty;
intid=0;
if(!int.TryParse(idString,outid))
{
return;
}
vardbeh=Common.DB.Factory.CreateDBEntityHelper();
if(int.TryParse(idString,outid))
{
vardlh=newDownLinkHelper();
stringdownlink=dlh.GetDownlink(id);
if(!string.IsNullOrEmpty(downlink))
{
Response.Redirect(Server.UrlPathEncode(Server.UrlDecode(downlink)));
}
}
[DownLinkHelper.cs]
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.Caching;
///
///DownLink的摘要说明
///
publicclassDownLinkHelper
{
Common.DB.IDBEntityHelperdbh=Common.DB.Factory.CreateDBEntityHelper();
publicstringGetDownlink(intval)
{
varent=dbh.GetEntity("SoftID="+val);
if(ent!=null)
{
returnent.Address;
}
returnstring.Empty;
}
}
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Data;
usingSystem.Data.Common;
usingSystem.Data.SqlClient;
///
///SQLServerHandler的摘要说明
///
publicclassDBHelper:IDBHelper
{
stringconnectionString;
publicstringConnectionString
{
get{returnconnectionString;}
set{connectionString=value;}
}
publicDBHelper(stringconnectionString)
{
this.connectionString=connectionString;
}
publicDbCommandCreateCommand()
{
returnnewSqlCommand();
}
publicDbConnectionCreateConnection()
{
returnnewSqlConnection(connectionString);
}
publicDbParameterCreateParameter(stringname,objectvalue)
{
DbParameterparameter=CreateParameter();
parameter.ParameterName=name;
parameter.Value=value;
returnparameter;
}
publicDbParameterCreateParameter(stringname)
{
DbParameterparameter=CreateParameter();
parameter.ParameterName=name;
returnparameter;
}
publicDbParameterCreateParameter()
{
returnnewSqlParameter();
}
publicTExecuteScalar(stringsql,paramsDbParameter[]parameters)
{
using(DbConnectionconnection=CreateConnection())
{
DbCommandcmd=CreateCommand();
cmd.Connection=connection;
cmd.CommandText=sql;
cmd.Parameters.AddRange(parameters);
connection.Open();
objecto=cmd.ExecuteScalar();
connection.Close();
return(T)Convert.ChangeType(o,typeof(T));
}
}
publicDbDataReaderExecuteReader(stringsql,paramsDbParameter[]parameters)
{
DbConnectionconnection=CreateConnection();
DbCommandcmd=CreateCommand();
cmd.Connection=connection;
cmd.CommandText=sql;
cmd.Parameters.AddRange(parameters);
connection.Open();
returncmd.ExecuteReader(CommandBehavior.CloseConnection);
}
publicintExecuteNoneQuery(stringsql,paramsDbParameter[]parameters)
{
using(DbConnectionconnection=CreateConnection())
{
DbCommandcmd=CreateCommand();
cmd.Connection=connection;
cmd.CommandText=sql;
cmd.Parameters.AddRange(parameters);
connection.Open();
intnum=cmd.ExecuteNonQuery();
connection.Close();
returnnum;
}
}
publicListGetDataList(stringsql,paramsDbParameter[]parameters)
{
Listlist=newList();
using(DbDataReaderreader=ExecuteReader(sql,parameters))
{
while(reader.Read())
{
Hashtablehs=newHashtable();
for(inti=0;i{
hs[reader.GetName(i)]=reader.GetValue(i);
}
list.Add(hs);
}
reader.Close();
reader.Dispose();
}
returnlist;
}
publicHashtableGetData(stringsql,paramsDbParameter[]parameters)
{
Hashtablehs=null;
using(DbDataReaderreader=ExecuteReader(sql,parameters))
{
while(reader.Read())
{
hs=newHashtable();
for(inti=0;i{
hs[reader.GetName(i)]=reader.GetValue(i);
}
}
reader.Close();
reader.Dispose();
}
returnhs;
}
}
///
///DbEntityHelper的摘要说明
///
publicclassDBEntityHelper:IDBEntityHelper
{
IDBHelperdb=null;
publicIDBHelperDBHelper
{
get{returndb;}
set{db=value;}
}
publicDBEntityHelper()
{
}
publicTGetEntityBySql(stringsql,paramsDbParameter[]parameters)
{
Listlist=GetDataList(sql,parameters);
if(list!=null)
{
if(list.Count==1)
{
returnlist[0];
}
}
returndefault(T);
}
publicTGetEntity(intid)
{
returnGetEntityById(id);
}
publicTGetEntity(IDTid)
{
returnGetEntityById(id);
}
publicTGetEntityById(IDTid)
{
stringidFieldName=FieldAttribute.GetIDFieldName(typeof(T));
if(!string.IsNullOrEmpty(idFieldName))
{
returnGetEntity(idFieldName+"=@ID",db.CreateParameter("@ID",id));
}
returndefault(T);
}
publicTGetEntity(stringwhere,paramsDbParameter[]parameters)
{
returnGetEntityByWhere(where,parameters);
}
publicTGetEntity(stringwhere,stringsort,paramsDbParameter[]parameters)
{
returnGetEntityByWhere(where,sort,parameters);
}
publicTGetEntityByWhere(stringwhere,paramsDbParameter[]parameters)
{
returnGetEntityByWhere(where,null,parameters);
}
publicTGetEntityByWhere(stringwhere,stringsort,paramsDbParameter[]parameters)
{
Listlist=GetDataList(1,where,sort,parameters);
if(list!=null)
{
if(list.Count==1)
{
returnlist[0];
}
}
returndefault(T);
}
///
///sql获得列表
///
///
///
///
///
publicListGetDataList(stringqueryString,paramsDbParameter[]parameters)
{
queryString=queryString.Trim();
PropertyInfo[]propertys=typeof(T).GetProperties();
stringsql=null;
if(queryString.IndexOf("select",StringComparison.OrdinalIgnoreCase)==0)
{
sql=queryString;
}
else
{
Typetyp=typeof(T);
stringtableName=TableAttribute.GetTableName(typ);
StringBuilderfields=newStringBuilder("0");
foreach(PropertyInfopiinpropertys)
{
fields.Append(",");
stringfsql=FieldAttribute.GetFieldSQL(pi);
if(!string.IsNullOrEmpty(fsql))
{
fields.Append(fsql);
}
else
{
fields.Append(FieldAttribute.GetFieldName(pi));
}
}
if(queryString.IndexOf("where",StringComparison.OrdinalIgnoreCase)==0)
{
sql="selecttop1000"+fields.ToString()+"from"+tableName+"with(nolock)"+queryString;
}
else
{
sql="selecttop1000"+fields.ToString()+"from"+tableName+"with(nolock)where"+queryString;
}
}
Listlist=newList(30);
using(DbDataReaderreader=db.ExecuteReader(sql,parameters))
{
while(reader.Read())
{
Tins=Activator.CreateInstance();
foreach(PropertyInfopiinpropertys)
{
intinx=reader.GetOrdinal(FieldAttribute.GetFieldName(pi).Replace("[",string.Empty).Replace("]",string.Empty));
objectvalue=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();
}
returnlist;
}
///
///获得列表
///
///
///选择多少条,如果num<=0则选择全部
///
///
///
///
publicListGetDataList(intnum,stringwhere,stringsort,paramsDbParameter[]parameters)
{
Typetyp=typeof(T);
stringtableName=TableAttribute.GetTableName(typ);
PropertyInfo[]propertys=typ.GetProperties();
StringBuilderfields=newStringBuilder("0");
foreach(PropertyInfopiinpropertys)
{
fields.Append(",");
stringfsql=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="orderby"+sort;
}
stringsql=null;
if(num>0)
{
sql="selecttop"+num+""+fields.ToString()+"from"+tableName+"with(nolock)"+where+""+sort;
}
else
{
sql="select"+fields.ToString()+"from"+tableName+"with(nolock)"+where+""+sort;
}
returnGetDataList(sql,parameters);
}
///
///选择全部数据列表
///
///
///
publicListGetDataList()
{
returnGetDataList(100,null,null);
}
///
///获得一个分页数据列表
///
///
///
///
///
///
///
///
///
publicListGetPageDataList(intpageNumber,intpageSize,stringwhere,stringsort,outintrecordCount,paramsDbParameter[]parameters)
{
Typetyp=typeof(T);
stringtableName=TableAttribute.GetTableName(typ);
PropertyInfo[]propertys=typ.GetProperties();
StringBuilderfields=newStringBuilder("0");
stringidentity=string.Empty;
foreach(PropertyInfopiinpropertys)
{
if(FieldAttribute.IsIdentity(pi))identity=FieldAttribute.GetFieldName(pi);
fields.Append(",");
stringfsql=FieldAttribute.GetFieldSQL(pi);
if(!string.IsNullOrEmpty(fsql))
{
fields.Append(fsql);
}
else
{
fields.Append(FieldAttribute.GetFieldName(pi));
}
//fields.Append(FieldAttribute.GetFieldName(pi));
}
QueryStringHelperquery=newQueryStringHelper();
query.Fields=fields.ToString();
query.Identity=identity;
query.Table=tableName;
query.PageSize=pageSize;
query.AbsolutePage=pageNumber;
query.Where=where;
query.Sort=sort;
DbParameter[]cps=newDbParameter[parameters.Length];
for(inti=0;i{
DbParameterpara=parameters[i];
cps[i]=db.CreateParameter(para.ParameterName,para.Value);
}
recordCount=db.ExecuteScalar(query.GetCountQueryString(),cps);
stringselectString=query.GetQueryString();
returnGetDataList(selectString,parameters);
}
///
///修改数据
///
///
///
///
publicboolUpdateData(Tentity)
{
Typetyp=typeof(T);
PropertyInfo[]propertys=typeof(T).GetProperties();
stringtableName=TableAttribute.GetTableName(typ);
Listlist=newList(propertys.Length);
StringBuilderfps=newStringBuilder();
boolfirst=true;
stringwhere=string.Empty;
foreach(PropertyInfopiinpropertys)
{
if(FieldAttribute.IsAllowModifyUndefined(pi))
{
continue;
}
if(!FieldAttribute.IsIdentity(pi))
{
Typepp=pi.PropertyType;
objectvalue=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)));
}
}
intnum=db.ExecuteNoneQuery("update"+tableName+"set"+fps+"where"+where,list.ToArray());
if(num==1)
{
returntrue;
}
returnfalse;
}
///
///插入数据
///
///
///
///
publicboolInsertData(Tentity)
{
Typetyp=typeof(T);
PropertyInfo[]propertys=typeof(T).GetProperties();
stringtableName=TableAttribute.GetTableName(typ);
Listlist=newList(propertys.Length);
StringBuilderfields=newStringBuilder();
StringBuilderparames=newStringBuilder();
boolfirst=true;
foreach(PropertyInfopiinpropertys)
{
if(!FieldAttribute.IsIdentity(pi))
{
Typepp=pi.PropertyType;
objectvalue=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));
}
}
intnum=db.ExecuteNoneQuery("insertinto"+tableName+"("+fields+")values("+parames+")",list.ToArray());
if(num==1)
{
returntrue;
}
returnfalse;
}
///
///插入数据,并返回最终插入的ID
///
///
///
///
///
publicRTInsertDataAndGetID(Tentity)
{
Typetyp=typeof(T);
PropertyInfo[]propertys=typeof(T).GetProperties();
stringtableName=TableAttribute.GetTableName(typ);
Listlist=newList(propertys.Length);
StringBuilderfields=newStringBuilder();
StringBuilderparames=newStringBuilder();
boolfirst=true;
foreach(PropertyInfopiinpropertys)
{
if(!FieldAttribute.IsIdentity(pi))
{
Typepp=pi.PropertyType;
objectvalue=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));
}
}
stringsql="insertinto"+tableName+"("+fields+")values("+parames+");select@@identity;";
returndb.ExecuteScalar(sql,list.ToArray());
}
}
拉丁的传说
浏览 725回答 2
2回答

慕少森

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

冉冉说

vardbeh=Common.DB.Factory.CreateDBEntityHelper();这句有什么作用?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript