1、上课日志20171020一、复习、为实例2-2创建数据实体类1、复习: 配置文件添加连接数据库字符串 添加工具类SqlHelper 如何获取配置文件的连接数据库字符串 为SqlHelper添加方法2、什么是实体类:通常类名与数据表名称一致,该类中包含一系列属性,这些属性与数据库中的字段一一对应,从数据库中查询出来的数据都使用该类的对象来保存,以便在程序中使用。(如何理解?)3、数据实体类作用:封装数据,数据封装到类的对象中(一条记录就是一个该类型的对象,多条记录就是该类型对象的集合)泛型集合Listclass Category public int TId get; set; public string TName get; set; public string TParentId get; set; public string TNote get; set; class ContentInfo public int DId get; set; public int DTId get; set; public string DName get; set; public string D
2、Content get; set; 二、为实例2-2创建根据指定的父Id查询对应的子类别,返回值为对应子类别集合的方法/根据指定的父Id查询对应的子类别,返回值为对应子类别集合提问:泛型集合? List private List GetDataByParentId(int pid) List list = new List(); string sql = select tid,tname from Category where tparentId=pid; /SqlParameter par = new SqlParameter(pid, pid); /using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, par) using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter(pid, pid) if (reader.HasRows) while (reader.Read() Category model = new Category
3、(); model.TId = reader.GetInt32(0); model.TName = reader.GetString(1); list.Add(model); return list; 形象比喻讲解三、为实例2-2创建加载类别信息到TreeViewTreeNodeCollection类用于存储和管理TreeView控件中的TreeNode对象的集合。private void LoadCategory(List listRoot, TreeNodeCollection treeNodeCollection) /循环集合listRoot中的每条数据加载到treeNodeCollection foreach (var mode in listRoot) TreeNode tnode = treeNodeCollection.Add(mode.TName); /tnode就是刚刚增加的节点 tnode.Tag = mode.TId; LoadCategory(GetDataByParentId(mode.TId), tnode.Nodes);/递归调用,从上到下为TreeVie
4、w加载节点 private void Form1_Load(object sender, EventArgs e) List listRoot = GetDataByParentId(-1); /加载类别信息到TreeView LoadCategory(listRoot, treeView1.Nodes); 抽象:必要时画动态图形象说明!四、为实例2-2加载文章名称1. /根据类别id,查询对应类别下的所有的文章对象 private List GetContentInfoByCategoryId(int cateId) List list = new List(); string sql = select did,dName from ContentInfo where dtid=tid; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter(tid, cateId) if (reader.HasRows) while (reader.Read() ContentInfo model =
5、new ContentInfo(); model.DId = reader.GetInt32(0); model.DName = reader.GetString(1); list.Add(model); return list; 2. /当选择treeView1控件某节点后的事件private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) listBox1.Items.Clear(); textBox1.Text = string.Empty; /当选中某个项时触发该事件 /获取当前选中项的id int tid = (int)treeView1.SelectedNode.Tag; List list = GetContentInfoByCategoryId(tid); foreach (ContentInfo item in list) /listBox1.Items.Add(item.DName); listBox1.Items.Add(item); listBox1.DisplayMember = D
6、Name; 五、为实例2-2展示文章内容private string GetContentByDid(int did) string sql = select dContent from ContentInfo where did=did; string content = Convert.ToString(SqlHelper.ExecuteScalar(sql, new SqlParameter(did, did); return content; private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) if (listBox1.SelectedItem != null) ContentInfo model = listBox1.SelectedItem as ContentInfo; if (model != null) int did = model.DId; /这里要根据文章的Id,查询文章的内容 textBox1.Text = GetContentByDid(did); 提问:as ContentInfo;作用?六、第五次课作业布置及要求头脑风暴:如果使用的是其他类型的数据库(不是Sql Server),SqlHelper还能使用吗?如何用?作业:1、完善文章阅读器实例,要求录屏实例运行效果(格式为MP4),同实例完整源代码一起上交2、模仿文章阅读器实例,尝试开发一个图片浏览器,要求录屏实例运行效果(格式为MP4),同实例完整源代码一起上交主要知识点:数据实体类创建及应用、泛型集合的应用、TreeView应用(实例:文章阅读器2)
《课程2上课日志4》由会员 y****g分享,可在线阅读,更多相关《课程2上课日志4》请在金锄头文库上搜索。