小编今天遇到了一个问题,查一条数据的问题,小编最初使用了List<实体>,虽然这样也可以但是毕竟这样不规范,经验匮乏的我还被师傅给训了一顿(诸如:你咋做的这么慢,这个小问题搞了一天?),受训归受训,小编还是很崇拜强者的,你比我强,你训我,我接受,你比我弱,你训我,那你估计是想上天!
1.查询一条数据,返回值类型要使用实体
public OAOilCardShopRecordEntity QueryBeforeSaveDeposit(int ID)
{
OAOilCardShopRecordEntity result = new OAOilCardShopRecordEntity();
//Hashtable resHt = new Hashtable();
string strSql = @"select a.*,b.OilCardNum,c.UserName from Tbl_OA_OilCardChargeRecord as a inner join
Tbl_OA_OilCard as b on a.OilID=b.ID left join Tbl_SysUser as c on c.LoginName=a.OPID
where a.ID = @ID";
try
{
using (DbCommand cmd = conn.GetSqlStringCommond(strSql))
{
if (!ID.Equals(0))
{
conn.AddInParameter(cmd, "@ID", DbType.Int32, ID);
}
using (DataTable dt = conn.ExecuteDataTable(cmd))
{
foreach (DataRow idrCount in dt.Rows)
{
result.ID = Convert.ToInt32(idrCount["ID"]);
result.OilCardNum = Convert.ToString(idrCount["OilCardNum"]);
result.BeforeMoney = Convert.ToDecimal(idrCount["BeforeMoney"]);
result.ShopMoney = Convert.ToDecimal(idrCount["ShopMoney"]);
result.OverMoney = Convert.ToDecimal(idrCount["OverMoney"]);
result.OPID = Convert.ToString(idrCount["OPID"]);
result.Remark = Convert.ToString(idrCount["Remark"]);
result.UserName = Convert.ToString(idrCount["UserName"]);
}
}
}
}
catch (ApplicationException ex)
{
throw new ApplicationException(ex.Message);
}
return result;
}
2.查询一个列表,返回值类型要使用List
/// <summary>
/// 查询油卡管理数据用于导出
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public List<OAOilCardEntity> QueryOilCardExport(OAOilCardEntity entity)
{
List<OAOilCardEntity> result = new List<OAOilCardEntity>();
Hashtable resHT = new Hashtable();
try
{
string strSQL = @" select a.*,b.UserName FROM Tbl_OA_OilCard as a left join Tbl_SysUser as b on a.OPID=b.LoginName WHERE (1=1) ";
if (!string.IsNullOrEmpty(entity.DelFlag)) { strSQL += "and a.DelFlag='" + entity.DelFlag + "'"; }
if (!string.IsNullOrEmpty(entity.OilCardPlat)) { strSQL += "and a.OilCardPlat='" + entity.OilCardPlat + "'"; }
if (!string.IsNullOrEmpty(entity.OilCardNum)) { strSQL += "and a.OilCardNum like '%" + entity.OilCardNum + "%'"; }
if (!string.IsNullOrEmpty(entity.TruckNum)) { strSQL += "and a.TruckNum like '%" + entity.TruckNum + "%'"; }
if (!string.IsNullOrEmpty(entity.OilCardName)) { strSQL += "and a.OilCardName like '%" + entity.OilCardName + "%'"; }
if (!entity.OilWear.Equals(0)) { strSQL += " and a.OverMoney < a.WarnMoney "; }
if (!entity.DepID.Equals(0)) { strSQL += " and a.DepID=" + entity.DepID + ""; }
if ((entity.StartDate.ToString("yyyy-MM-dd") != "0001-01-01" && entity.StartDate.ToString("yyyy-MM-dd") != "1900-01-01"))
{
strSQL += " and a.OP_DATE>='" + entity.StartDate.ToString("yyyy-MM-dd") + "'";
}
if ((entity.EndDate.ToString("yyyy-MM-dd") != "0001-01-01" && entity.EndDate.ToString("yyyy-MM-dd") != "1900-01-01"))
{
strSQL += " and a.OP_DATE<'" + entity.EndDate.AddDays(1).ToString("yyyy-MM-dd") + "'";
}
using (DbCommand command = conn.GetSqlStringCommond(strSQL))
{
using (DataTable dt = conn.ExecuteDataTable(command))
{
foreach (DataRow idr in dt.Rows)
{
result.Add(new OAOilCardEntity
{
ID = Convert.ToInt32(idr["ID"]),
OilCardNum = Convert.ToString(idr["OilCardNum"]),
OilCardName = Convert.ToString(idr["OilCardName"]),
OverMoney = Convert.ToDecimal(idr["OverMoney"]),
DebitMoney = Convert.ToDecimal(idr["DebitMoney"]),
FixMoney = Convert.ToDecimal(idr["FixMoney"]),
WarnMoney = Convert.ToDecimal(idr["WarnMoney"]),
TruckNum = Convert.ToString(idr["TruckNum"]),
OilCardPlat = Convert.ToString(idr["OilCardPlat"]),
DelFlag = Convert.ToString(idr["DelFlag"]),
CardType = Convert.ToString(idr["CardType"]),
OP_DATE = Convert.ToDateTime(idr["OP_DATE"]),
UserName = Convert.ToString(idr["UserName"]),
DepName = Convert.ToString(idr["DepName"])
});
}
}
}
}
catch (ApplicationException ex) { throw new ApplicationException(ex.Message); }
return result;
}
两者之间有着很多不同之处,要仔细看。