我在一个静态类中有一个数据阅读器,它允许我从数据库中获取单个或多个记录。这在 ASP.net 应用程序中使用。但是,我们最近遇到了一些奇怪的问题,我想知道我当前的方法结构是否可能是问题的原因。有时我会收到一条错误消息,指出数据读取器仍处于打开状态。其他时候会发生服务器超时。
这是我的一个从数据库获取数据的静态类的示例。为了简洁起见,我将这个例子中的方法数量减少到我的“get”结构。因此,如果我想返回一个引用类型的列表,我会将以下表单称为 aspx.cs 文件。
List<Reference> ref = ReferenceAdapter.GetAllByType(......);
非常感谢任何见解。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using DbProvider;
using MyBusinessModelLayer;
namespace MyDataAccessLayer
{
public static class ReferenceAdapter //: IReferenceAdapter
{
static IDbProvider _dbProvider;
static ReferenceAdapter()
{
Initialize();
}
private static void Initialize()
{
_dbProvider = new SqlDbProvider(Config.SqlConnectionString);
}
public static List<Reference> GetAllByType(ReferenceType referenceType)
{
return GetAllByType(referenceType, false);
}
public static List<Reference> GetAllByType(ReferenceType referenceType, bool isIncludeDeleted)
{
Hashtable hIn, hOut;
string sp;
sp = "[ReferenceSelectAllByReferenceType]";
hIn = new Hashtable();
hIn.Add("@ReferenceType", referenceType.ToString());
if (!isIncludeDeleted)
{
hIn.Add("@Deleted", false);
}
hOut = new Hashtable();
hOut.Add("@RTNStatus", 0);
return GetMultipleFromDb(sp, hIn, hOut, CommandType.StoredProcedure);
}
private static List<Reference> GetMultipleFromDb(string sSql, Hashtable hInputParameters)
{
return GetMultipleFromDb(sSql, hInputParameters, new Hashtable(), new CommandType());
}
private static List<Reference> GetMultipleFromDb(string sSql, Hashtable hInputParameters, CommandType cType)
{
return GetMultipleFromDb(sSql, hInputParameters, new Hashtable(), cType);
}
private static List<Reference> GetMultipleFromDb(string sSql, Hashtable hInputParameters, Hashtable hOutputParameters, CommandType cType)
{
IDbCommand dbCommand = _dbProvider.Select(sSql, hInputParameters, hOutputParameters, cType);
var lo = new List<Reference>();
using (IDataReader dr = dbCommand.ExecuteReader())
{
while (dr.Read())
lo.Add(GetObjectFromDataRecord(dr));
}
dbCommand.Connection.Close();
return lo;
}// GetMultipleFromDb()
private static Hashtable GetParametersFromObject(Reference o)
{
Hashtable h = new Hashtable();
h.Add("@ReferenceShortName", o.ShortName);
h.Add("@ReferenceLongName", o.LongName);
h.Add("@ReferenceDescription", o.Description);
h.Add("@ReferenceType", o.Type.ToString());
h.Add("@ReferenceSortKey", o.SortKey);
return h;
}// GetParametersFromObject()
private static Reference GetObjectFromDataRecord(IDataRecord dr)
{
Reference o = new Reference();
o.ReferenceId = dr["ReferenceID"].DBNullToInt();
o.ShortName = dr["ReferenceShortName"].ToString();
o.LongName = dr["ReferenceLongName"].ToString();
o.Description = dr["ReferenceDescription"].ToString();
o.Type = (ReferenceType)Enum.Parse(typeof(ReferenceType), dr["ReferenceType"].ToString(), true);
o.SortKey = dr["ReferenceSortKey"].DBNullToInt();
o.Created = dr["CreatedDate"].DBNullToDateMinVal();
o.CreatedBy = dr["CreatedBy"].DBNullToInt();
o.Updated = dr["UpdatedDate"].DBNullToDateMinVal();
o.UpdatedBy = dr["UpdatedBy"].DBNullToInt();
o.Deleted = Convert.ToBoolean(dr["Deleted"]);
o.Deletable = GetAssetReferenceCount(o.Type, o.ReferenceId);
return o;
}// GetObjectFromDataRecord()
}
}