项目文件列表:
文件中值得注意的代码:
DataManager.cs:
public List<TableModel> GetDataList() { List<TableModel> models; SqlHelper sqlhelper = new SqlHelper(); string sql = "select * from MyOrders"; using (SqlDataReader reader = sqlhelper.ExecuteReader(sql, null)) { if (reader.HasRows) { models = new List<TableModel>(); while (reader.Read()) { TableModel model = new TableModel(); model.ID = int.Parse(reader[0].ToString()); model.Name = reader[1].ToString(); model.Number = reader[2].ToString(); model.Count = reader.IsDBNull(3) ? null : (int?)reader.GetInt32(3); model.Price = reader.GetInt32(4); model.Purchaser = reader.IsDBNull(5) ? string.Empty : reader.GetString(5); model.DateTime = reader.IsDBNull(6) ? null : (DateTime?)reader.GetDateTime(6); model.Salesman = reader.IsDBNull(7) ? string.Empty : reader[7].ToString(); models.Add(model); } return models; } } return null; }
TableModel.cs
public class TableModel { /// <summary> /// 数据模型: /// 像DataGridView一样的数据绑定控件,只认识属性(Model中的属性名称), /// 不识别(认)字段。与表中的列的名称不是一回事.但是可以获得表中的列名, /// 并设置在DataGridView中的列名上。 /// </summary> public TableModel() { } /// <summary> /// id /// </summary> public int ID { get; set; } /// <summary> /// 商品名称 /// </summary> public string Name { get; set; } /// <summary> /// 商品编号 /// </summary> public string Number { get; set; } /// <summary> /// 销售数量 /// </summary> public int? Count { get; set; } /// <summary> /// 销售价格 /// </summary> public int Price { get; set; } /// <summary> /// 购买人 /// </summary> public string Purchaser { get; set; } /// <summary> /// 销售日期 /// </summary> public DateTime? DateTime { get; set; } /// <summary> /// 销售员 /// </summary> public string Salesman { get; set; } }