此数据查询工具界面如下图所示:

tmpF36E

此工具采用MySoft.Data中的QueryCreator(查询创建器),QueryCreator可以创建任意组合的数据库查询,包括关联查询,选择字段,添加条件等。

    QueryCreator最大的好处就是可以进行非泛型查询,还可以满足用户自定义查询的需求。

现在用户所使用的ORM有一个这样的问题,都需求先定义好实体,然后才对其进行查询,当然,通过实体操作,MySoft.Data可以这样做,例如:

首先定义一个接口:

    public interface Message : IEntity
    {
        [PrimaryKey]
        int ID { get; }

        string AdminFeedback { get; set; }

        DateTime AdminFeedbackTime { get; set; }

        string Content { get; set; }

        string Email { get; set; }

        bool IsPrivate { get; set; }

        string Nick { get; set; }

        DateTime PostTime { get; set; }
    }

 

通过生成工具生成实体:

    /// <summary>
    /// 表名:Message
    /// </summary>
    [SerializableAttribute()]
    public partial class Message : Entity
    {

        protected Int32 _ID;

        protected String _AdminFeedback;

        protected DateTime _AdminFeedbackTime;

        protected String _Content;

        protected String _Email;

        protected Boolean _IsPrivate;

        protected String _Nick;

        protected DateTime _PostTime;

        public Int32 ID
        {
            get
            {
                return this._ID;
            }
            set
            {
                this.OnPropertyValueChange(_.ID, _ID, value);
                this._ID = value;
            }
        }

        public String AdminFeedback
        {
            get
            {
                return this._AdminFeedback;
            }
            set
            {
                this.OnPropertyValueChange(_.AdminFeedback, _AdminFeedback, value);
                this._AdminFeedback = value;
            }
        }

        public DateTime AdminFeedbackTime
        {
            get
            {
                return this._AdminFeedbackTime;
            }
            set
            {
                this.OnPropertyValueChange(_.AdminFeedbackTime, _AdminFeedbackTime, value);
                this._AdminFeedbackTime = value;
            }
        }

        public String Content
        {
            get
            {
                return this._Content;
            }
            set
            {
                this.OnPropertyValueChange(_.Content, _Content, value);
                this._Content = value;
            }
        }

        public String Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this.OnPropertyValueChange(_.Email, _Email, value);
                this._Email = value;
            }
        }

        public Boolean IsPrivate
        {
            get
            {
                return this._IsPrivate;
            }
            set
            {
                this.OnPropertyValueChange(_.IsPrivate, _IsPrivate, value);
                this._IsPrivate = value;
            }
        }

        public String Nick
        {
            get
            {
                return this._Nick;
            }
            set
            {
                this.OnPropertyValueChange(_.Nick, _Nick, value);
                this._Nick = value;
            }
        }

        public DateTime PostTime
        {
            get
            {
                return this._PostTime;
            }
            set
            {
                this.OnPropertyValueChange(_.PostTime, _PostTime, value);
                this._PostTime = value;
            }
        }

        /// <summary>
        /// 获取实体对应的表名
        /// </summary>
        protected override Table GetTable()
        {
            return new Table<Message>("Message");
        }

        /// <summary>
        /// 获取实体中的标识列
        /// </summary>
        protected override Field GetIdentityField()
        {
            return _.ID;
        }

        /// <summary>
        /// 获取实体中的主键列
        /// </summary>
        protected override Field[] GetPrimaryKeyFields()
        {
            return new Field[] {
                        _.ID,
                        };
        }

        /// <summary>
        /// 获取列信息【:ID 1: AdminFeedback 2: AdminFeedbackTime 3: Content 4: Email 5: IsPrivate 6: Nick 7: PostTime】
        /// </summary>
        protected override Field[] GetFields()
        {
            return new Field[] {
                        _.ID,
                        _.AdminFeedback,
                        _.AdminFeedbackTime,
                        _.Content,
                        _.Email,
                        _.IsPrivate,
                        _.Nick,
                        _.PostTime};
        }

        /// <summary>
        /// 获取列数据【:_ID 1: _AdminFeedback 2: _AdminFeedbackTime 3: _Content 4: _Email 5: _IsPrivate 6: _Nick 7: _PostTime】
        /// </summary>
        protected override object[] GetValues()
        {
            return new object[] {
                        this._ID,
                        this._AdminFeedback,
                        this._AdminFeedbackTime,
                        this._Content,
                        this._Email,
                        this._IsPrivate,
                        this._Nick,
                        this._PostTime};
        }

        /// <summary>
        /// 给当前实体赋值
        /// </summary>
        protected override void SetValues(IRowReader reader)
        {
            if ((false == reader.IsDBNull(_.ID)))
            {
                this._ID = reader.GetInt32(_.ID);
            }
            if ((false == reader.IsDBNull(_.AdminFeedback)))
            {
                this._AdminFeedback = reader.GetString(_.AdminFeedback);
            }
            if ((false == reader.IsDBNull(_.AdminFeedbackTime)))
            {
                this._AdminFeedbackTime = reader.GetDateTime(_.AdminFeedbackTime);
            }
            if ((false == reader.IsDBNull(_.Content)))
            {
                this._Content = reader.GetString(_.Content);
            }
            if ((false == reader.IsDBNull(_.Email)))
            {
                this._Email = reader.GetString(_.Email);
            }
            if ((false == reader.IsDBNull(_.IsPrivate)))
            {
                this._IsPrivate = reader.GetBoolean(_.IsPrivate);
            }
            if ((false == reader.IsDBNull(_.Nick)))
            {
                this._Nick = reader.GetString(_.Nick);
            }
            if ((false == reader.IsDBNull(_.PostTime)))
            {
                this._PostTime = reader.GetDateTime(_.PostTime);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if ((obj == null))
            {
                return false;
            }
            if ((false == typeof(Message).IsAssignableFrom(obj.GetType())))
            {
                return false;
            }
            if ((((object)(this)) == ((object)(obj))))
            {
                return true;
            }
            return false;
        }

        public class _
        {

            /// <summary>
            /// 表示选择所有列,与*等同
            /// </summary>
            public static AllField All = new AllField<Message>();

            /// <summary>
            /// 字段名:ID - 数据类型:Int32
            /// </summary>
            public static Field ID = new Field<Message>("ID");

            /// <summary>
            /// 字段名:AdminFeedback - 数据类型:String
            /// </summary>
            public static Field AdminFeedback = new Field<Message>("AdminFeedback");

            /// <summary>
            /// 字段名:AdminFeedbackTime - 数据类型:DateTime
            /// </summary>
            public static Field AdminFeedbackTime = new Field<Message>("AdminFeedbackTime");

            /// <summary>
            /// 字段名:Content - 数据类型:String
            /// </summary>
            public static Field Content = new Field<Message>("Content");

            /// <summary>
            /// 字段名:Email - 数据类型:String
            /// </summary>
            public static Field Email = new Field<Message>("Email");

            /// <summary>
            /// 字段名:IsPrivate - 数据类型:Boolean
            /// </summary>
            public static Field IsPrivate = new Field<Message>("IsPrivate");

            /// <summary>
            /// 字段名:Nick - 数据类型:String
            /// </summary>
            public static Field Nick = new Field<Message>("Nick");

            /// <summary>
            /// 字段名:PostTime - 数据类型:DateTime
            /// </summary>
            public static Field PostTime = new Field<Message>("PostTime");
        }
    }
 

生成以上实体后,CURD操作就变得很简单了。

        #region 强类型操作

        //添加
        Message msg = new Message()
        {
            Content = "测试内容",
            PostTime = DateTime.Now
        };
        DbSession.Default.Save(msg);

        //修改
        msg.Content = "测试修改内容";
        DbSession.Default.Save(msg);

        //删除
        DbSession.Default.Delete(msg);

        //查询
        DbSession.Default.From<Message>()
            .Where(Message._.ID == 1)
            .OrderBy(Message._.ID.Desc)
            .ToList();

        #endregion

 

同样,通过创建器而不需要定义实体也可以实现同样的要求

        #region 创建器操作

        //添加
        InsertCreator ic = InsertCreator.NewCreator()
            .From("Message")
            .AddInsert("Content", "测试内容")
            .AddInsert("PostTime", DateTime.Now);
        DbSession.Default.Excute(ic);

        //修改
        UpdateCreator uc = UpdateCreator.NewCreator()
            .From("Message")
            .AddUpdate("Content", "测试修改内容")
            .AddWhere("ID", 1);
        DbSession.Default.Excute(ic);

        //删除
        DeleteCreator dc = DeleteCreator.NewCreator()
            .From("Message")
            .AddWhere("ID", 1);
        DbSession.Default.Excute(dc);

        //查询
        QueryCreator qc = QueryCreator.NewCreator()
            .From("Message")
            .AddWhere("ID", 1)
            .AddOrder("ID desc");
        DbSession.Default.From(qc).ToTable();

        #endregion

言归正传,上面只是说明一下QueryCreator的妙用,接下来继续介绍数据查询工具:

此查询工具实现了两种查询模式:单表查询与关联查询

条件可以根据表的字段自动生成,后面的值可以设置为字段对应的值,可以为数字,字符串或日期等类型。

一、单表查询:

自定义条件:

tmp6A26

定义一个查询条件CategoryID 为1 ,点击查询,将查出相应的结果

tmpA8EB

二、关联查询:

选择INNER JOIN,LEFT JOIN,RIGHT JOIN其中一种,会出现关联条件设置的按钮,点击可以设置两个表之间的关联关系

tmpF028

设置好相应条件,点击查询显示结果

tmp4432

到此,简单的查询工具已经完成了

需要更强大的功能,可以点击数据库查询分析器,如图:

tmpA20B

此分析器可以对数据进行增删除改查操作,左边点击右键会生成Select,Insert,Update,Delete相应的SQL语句,按F5可以对当前SQL进行操作!

 

下载MySoft数据查询工具 v1.0

下一章将介绍 MySoft.Data 实体开发步骤。

 

如有什么问题,请联系,或到MySoft问题反馈区进行反馈:

QQ:121849018

MSN:maoyong181@hotmail.com

Gmail:mysoft181@gmail.com


posted on 2010-03-04 13:35  MySoft  阅读(2001)  评论(4编辑  收藏  举报