sqldatareader 转实体类 不用反射
/// <summary>
/// 获得数据列表
/// </summary>
///'ProductId','SupplierId','CategoryId','CatalogId','Number','Name','Letter','MarketPrice','Price','Unit','Size','Origin','Packing','Material','Style','Certificate','OpenLight','ConsumerStrata','VIP','TaxInvoice','TaxRate','Content','DiscountIs','DiscountRate','DiscountTimeStart','DiscountTimeEnd','ChineseZodiac','Astro','Views','Sales','Amount','PostagePayer','PostageTemptId','Postage','Overpay','Visible','IsNew','State','Time','UpdateTim'
public IList<ProductMD> GetListArray(string fields, string where)
{
IList<ProductMD> list = new List<ProductMD>();
DbCommand dbCommand = DB.GetSqlStringCommand("select " + fields + " from db_Product where " + where);
using (IDataReader dr = DB.ExecuteReader(dbCommand))
{
while (dr.Read())
{
ProductMD md = new ProductMD();
#region 赋值给model
for (int i = 0; i < dr.FieldCount; i++)
{
if (dr.GetName(i).Equals("ProductId") && dr["ProductId"] != DBNull.Value)
{
md.ProductId = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("SupplierId") && dr["SupplierId"] != DBNull.Value)
{
md.SupplierId = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("CategoryId") && dr["CategoryId"] != DBNull.Value)
{
md.CategoryId = dr.GetString(i);
}
else if (dr.GetName(i).Equals("CatalogId") && dr["CatalogId"] != DBNull.Value)
{
md.CatalogId = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("Number") && dr["Number"] != DBNull.Value)
{
md.Number = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Name") && dr["Name"] != DBNull.Value)
{
md.Name = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Letter") && dr["Letter"] != DBNull.Value)
{
md.Letter = dr.GetString(i);
}
else if (dr.GetName(i).Equals("MarketPrice") && dr["MarketPrice"] != DBNull.Value)
{
md.MarketPrice = dr.GetDecimal(i);
}
else if (dr.GetName(i).Equals("Price") && dr["Price"] != DBNull.Value)
{
md.Price = dr.GetDecimal(i);
}
else if (dr.GetName(i).Equals("Unit") && dr["Unit"] != DBNull.Value)
{
md.Unit = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Size") && dr["Size"] != DBNull.Value)
{
md.Size = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Origin") && dr["Origin"] != DBNull.Value)
{
md.Origin = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Tag") && dr["Tag"] != DBNull.Value)
{
md.Tag = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Packing") && dr["Packing"] != DBNull.Value)
{
md.Packing = dr.GetByte(i);
}
else if (dr.GetName(i).Equals("Material") && dr["Material"] != DBNull.Value)
{
md.Material = dr.GetInt16(i);
}
else if (dr.GetName(i).Equals("Style") && dr["Style"] != DBNull.Value)
{
md.Style = dr.GetInt16(i);
}
else if (dr.GetName(i).Equals("Certificate") && dr["Certificate"] != DBNull.Value)
{
md.Certificate = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("OpenLight") && dr["OpenLight"] != DBNull.Value)
{
md.OpenLight = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("ConsumerStrata") && dr["ConsumerStrata"] != DBNull.Value)
{
md.ConsumerStrata = dr.GetString(i);
}
else if (dr.GetName(i).Equals("VIP") && dr["VIP"] != DBNull.Value)
{
md.VIP = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("TaxInvoice") && dr["TaxInvoice"] != DBNull.Value)
{
md.TaxInvoice = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("TaxRate") && dr["TaxRate"] != DBNull.Value)
{
md.TaxRate = dr.GetDecimal(i);
}
else if (dr.GetName(i).Equals("Content") && dr["Content"] != DBNull.Value)
{
md.Content = dr.GetString(i);
}
else if (dr.GetName(i).Equals("DiscountIs") && dr["DiscountIs"] != DBNull.Value)
{
md.DiscountIs = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("DiscountRate") && dr["DiscountRate"] != DBNull.Value)
{
md.DiscountRate = dr.GetDecimal(i);
}
else if (dr.GetName(i).Equals("DiscountTimeStart") && dr["DiscountTimeStart"] != DBNull.Value)
{
md.DiscountTimeStart = dr.GetDateTime(i);
}
else if (dr.GetName(i).Equals("DiscountTimeEnd") && dr["DiscountTimeEnd"] != DBNull.Value)
{
md.DiscountTimeEnd = dr.GetDateTime(i);
}
else if (dr.GetName(i).Equals("ChineseZodiac") && dr["ChineseZodiac"] != DBNull.Value)
{
md.ChineseZodiac = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Astro") && dr["Astro"] != DBNull.Value)
{
md.Astro = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Views") && dr["Views"] != DBNull.Value)
{
md.Views = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("Sales") && dr["Sales"] != DBNull.Value)
{
md.Sales = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("Amount") && dr["Amount"] != DBNull.Value)
{
md.Amount = dr.GetInt32(i);
}
else if (dr.GetName(i).Equals("PostagePayer") && dr["PostagePayer"] != DBNull.Value)
{
md.PostagePayer = dr.GetByte(i);
}
else if (dr.GetName(i).Equals("Postage") && dr["Postage"] != DBNull.Value)
{
md.Postage = dr.GetString(i);
}
else if (dr.GetName(i).Equals("Overpay") && dr["Overpay"] != DBNull.Value)
{
md.Overpay = dr.GetDecimal(i);
}
else if (dr.GetName(i).Equals("Visible") && dr["Visible"] != DBNull.Value)
{
md.Visible = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("IsNew") && dr["IsNew"] != DBNull.Value)
{
md.IsNew = dr.GetBoolean(i);
}
else if (dr.GetName(i).Equals("State") && dr["State"] != DBNull.Value)
{
md.State = dr.GetByte(i);
}
else if (dr.GetName(i).Equals("Time") && dr["Time"] != DBNull.Value)
{
md.Time = dr.GetDateTime(i);
}
else if (dr.GetName(i).Equals("UpdateTime") && dr["UpdateTime"] != DBNull.Value)
{
md.UpdateTime = dr.GetDateTime(i);
}
}
#endregion
list.Add(md);
}
}
return list;
}
posted on 2010-06-10 11:21 jianshaohui 阅读(953) 评论(0) 编辑 收藏 举报