GZFramwork数据库层《一》普通表增删改查

运行结果:

image

使用代码生成器(GZCodeGenerate)生成tb_MyUser的Model

生成器源代码下载地址:

https://github.com/GarsonZhang/GZCodeGenerate/

生成代码:

放在GZFramworkDB.Model项目下:

image

代码:

using GZFramwork.ORM;
using System.Data;

namespace GZFramworkDB.Model
{
    ///<summary>
    /// ORM模型, 数据表:tb_MyUser
    /// 来自:GarsonZhang
    /// </summary>
    [ORM_ObjectClassAttribute("tb_MyUser", "Account", "Account")]
    public sealed class tb_MyUser
    {
        public static string _TableName = "tb_MyUser";

        public static string _KeyName = "Account";


        /// <summary>
        /// 自增列
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.Int, 4, false, false)]
        public static string isid = "isid";

        /// <summary>
        /// 用户账号
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.VarChar, 20, true, true)]
        public static string Account = "Account";

        /// <summary>
        /// 用户名称
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.VarChar, 20, true, false)]
        public static string UserName = "UserName";

        /// <summary>
        /// 昵称
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.VarChar, 20, true, false)]
        public static string PetName = "PetName";

        /// <summary>
        /// 创建人
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.VarChar, 20, true, false)]
        public static string CreateUser = "CreateUser";

        /// <summary>
        /// 创建日期
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.DateTime, 8, true, false)]
        public static string CreateDate = "CreateDate";

        /// <summary>
        /// 修改人
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.VarChar, 20, true, false)]
        public static string LastUpdateUser = "LastUpdateUser";

        /// <summary>
        /// 修改日期
        /// </summary>
        [ORM_FieldAttribute(SqlDbType.DateTime, 8, true, false)]
        public static string LastUpdateDate = "LastUpdateDate";

    }

    public class _ORM_Export_tb_MyUser : ModelExport
    {
        public _ORM_Export_tb_MyUser()
        {
            typeModel = typeof(tb_MyUser);
        }
    }
}

设计Main项目主界面:

image

添加一个接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GZFramworkDB.Main.MyControls
{
    public interface IData
    {
        void DoSearch();
        void DoAdd();
        void DoDeleteKey();
        void DoDeleteTable();
        void DoUpdate();
    }
}

新增自定义控件:ucTableUnit

界面:

image

代码:

public partial class ucTableUnit : UserControl,IData
    {
        bllBusiness bll;
        public ucTableUnit()
        {
            InitializeComponent();
            bll = new bllBusiness(typeof(tb_MyUser));
        }

        public void DoSearch()
        {
            gridControl1.DataSource = bll.GetSummaryData();
            gridView1.BestFitColumns();//自动列宽
        }

        DataTable dtSource
        {
            get
            {
                if (gridControl1.DataSource == null) return null;
                return gridControl1.DataSource as DataTable;
            }
        }
        //新增
        public void DoAdd()
        {
            if (dtSource != null)
                dtSource.Rows.Add();
        }
        //主键删除,立即删除,无需提交
        public void DoDeleteKey()
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr != null)
            {
                string Keyvalue = dr[bll.SummaryKey].ToString();
                if (bll.Delete(Keyvalue) == true)
                {
                    dtSource.Rows.Remove(dr);
                }
            }
        }
        //缓存表删除,需要提交生效
        public void DoDeleteTable()
        {
            gridView1.DeleteSelectedRows();
        }
        //提交
        public void DoUpdate()
        {
            bll.Update(dtSource);
        }
    }

说明:

bllBusiness bll = new bllBusiness(typeof(tb_MyUser));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ORM_Main">主表ORM</param>
        /// <param name="DocCode">如果是单据,这里是单据标示</param>
        /// <param name="Length">单据长度</param>
        /// <param name="ORM_Details">明细表ORM</param>
        public bllBusiness(Type ORM_Main, string DocCode, int Length, params Type[] ORM_Details)
        {
            _DAL = new GZFramwork.Lib.GZdalBaseBusiness(ORM_Main, DocCode, Length, ORM_Details);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ORM_Main">主表ORM</param>
        /// <param name="ORM_Details">明细表ORM</param>
        public bllBusiness(Type ORM_Main, params Type[] ORM_Details)
        {
            _DAL = new GZFramwork.Lib.GZdalBaseBusiness(ORM_Main, null, 0, ORM_Details);
        }

补充完整frmMain.cs代码:

using GZFramworkDB.Main.MyControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GZFramworkDB.Main
{
    public partial class frmMain : Form
    {
        ucTableUnit TableUnit;//普通表操作

        IData uc;
        public frmMain()
        {
            InitializeComponent();

         TableUnit = new ucTableUnit() { Visible = false };
          pan_MyControls.Controls.Add(TableUnit);


        }

        private void menu_TableUnit_Click(object sender, EventArgs e)
        {
            foreach (Control col in pan_MyControls.Controls)
            {
                col.Visible = false;
            }

            TableUnit.Visible = true;

            uc = TableUnit;
        }

        #region 操作
        private void btn_Search_Click(object sender, EventArgs e)
        {
            if (uc != null)
                uc.DoSearch();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (uc != null)
            {
                uc.DoAdd();
            }
        }

        private void btn_DeleteKey_Click(object sender, EventArgs e)
        {
            if (uc != null)
            {
                uc.DoDeleteKey();
                MessageBox.Show("删除成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_DeleteTable_Click(object sender, EventArgs e)
        {
            if (uc != null)
            {
                uc.DoDeleteTable();
                MessageBox.Show("已经在缓存中删除行!
提交后生效", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (uc != null)
            {
                uc.DoUpdate();
                MessageBox.Show("提交成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion
    }
}

运行结果:

说明:

删除(主键)直接从数据库中删除数据

删除(缓存表)仅仅是删除了datatable,数据库中并没有删除,当点击提交后才会删除数据库

本系列项目源码下载地址:https://github.com/GarsonZhang/GZFramworkDBDemo/

生成器源码下载地址:https://github.com/GarsonZhang/GZCodeGenerate/

系列文章

1. GZFramwork数据库层《前言》Demo简介

2. GZFramwork数据库层《前言》DLL项目引用

3. GZFramwork数据库层《一》普通表增删改查

4. GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)

5. GZFramwork数据库层《三》普通主从表增删改查

6. GZFramwork数据库层《四》单据主从表增删改查(主键自动生成)

7. GZFramwork数据库层《五》高级主从表增删改查(主表明细表主键都自动生成)

8. GZFramwork数据库层《六》存储过程调用

9. GZFramwork数据库层《七》总结

慎于行,敏于思!GGGGGG
原文地址:https://www.cnblogs.com/GarsonZhang/p/4355117.html