.NET零基础入门10:打老鼠之数据存储

一:数据库设计

到此为止,打老鼠游戏还不能保存每次游戏的成绩,我们今天完成的任务就是要存储成绩到SQLSERVER的数据库中。

在上节课中,我们已经知道了如何创建数据库,所有,先创建数据库“MouseGame”,然后,按如下的数据格式建立一个数据表(表名:GameRecord):

wps_clip_image-19041

 

二:数据库读写

数据库的读写部分,请查看下节视频。最终,我们的成绩查看界面如下:

wps_clip_image-19185

 

三:视频

非公开部分,请联系最课程(www.zuikc.com

 

四:将DataRow转换成Model

在上面的视频中,我们的数据库直接以Datatable的形式赋值给了前台(UI)。在实际的应用的当中,尤其是多层架构的应用当中,我们更常用的做法是将数据库记录以List<Model>(还记得List<Model>这个形式吗?我们的前台UI的Code-behind代码中,我们存储老鼠控件,用了List<PictureBox>)的形式在前后台中间进行传递,即:

下面的代码:

public DataTable GetList()

{

    string sql = "select * from GameRecord";

    return this.GetTable(sql);

}

最好变成:

public List<GameRecord> GetRecordList()

{

    return DatatableHelper.ToList<GameRecord>(this.GetList());

}

GameRecord是什么?就是我们的游戏记录的实体类,它的实现如下:

public class GameRecord

{

    #region Public Properties

    public DateTime GameDateTime { get; set; }

    public int GameLevel { get; set; }

    public int Score { get; set; }

    public int Total { get; set; }

    #endregion

}

在上面的代码中,DatatableHelper的实现如下:

namespace GameSqlserverDal

{

    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Reflection;

    internal class DatatableHelper

    {

        #region Public Methods and Operators

        public static DataTable ToDataTable<T>(IEnumerable<T> list)

        {

            var pList = new List<PropertyInfo>();

            Type type = typeof(T);

            var dt = new DataTable();

            Array.ForEach(

                type.GetProperties(),

                p =>

                    {

                        pList.Add(p);

                        dt.Columns.Add(p.Name, p.PropertyType);

                    });

            foreach (T item in list)

            {

                DataRow row = dt.NewRow();

                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));

                dt.Rows.Add(row);

            }

            return dt;

        }

        public static List<T> ToList<T>(DataTable dt) where T : class, new()

        {

            var prlist = new List<PropertyInfo>();

            Type type = typeof(T);

            Array.ForEach(

                type.GetProperties(),

                p =>

                    {

                        if (dt.Columns.IndexOf(p.Name) != -1)

                        {

                            prlist.Add(p);

                        }

                    });

            var oblist = new List<T>();

            foreach (DataRow row in dt.Rows)

            {

                var ob = new T();

                prlist.ForEach(

                    p =>

                        {

                            if (row[p.Name] != DBNull.Value)

                            {

                                p.SetValue(ob, row[p.Name], null);

                            }

                        });

                oblist.Add(ob);

            }

            return oblist;

        }

        #endregion

    }

}

以我们当前的知识储备能力,我们还不能很好的理解上面这个帮助类的代码,但是没有关系,虽然我们目前写不出这个代码,但是我们一定要会用。就像我们写不出.NET Famework的API,但是我们会用好它,也是一种能力。现在,重构我们的代码,用List<GameRecord>来给我们的UI进行赋值吧。

posted @ 2015-04-08 10:55  陆敏技  阅读(752)  评论(0编辑  收藏  举报
Web Counter
Coupon for Contacts