ADO,net 实体数据模型增、删、改,浅谈

 

 

第一步:建立ADO.net数据模型,一步步操作就行

第二步:画个简单的测试界面

第三步铺代码

using DevComponents.DotNetBar.SuperGrid;
using DevComponents.DotNetBar.SuperGrid.Style;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ado增删改
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void StyleSPG()
        {
            GridPanel gpnl = SPG.PrimaryGrid;
            gpnl.AllowRowInsert = false;
            //gpnl.ColumnAutoSizeMode = ColumnAutoSizeMode.AllCells;//卡顿原因
            gpnl.KeyboardEditMode = KeyboardEditMode.EditOnKeystroke;//编辑模式
            gpnl.MouseEditMode = MouseEditMode.DoubleClick;
            gpnl.GroupByRow.Visible = false;//手动分组显示
            gpnl.ColumnHeader.FilterImageVisibility = ImageVisibility.Never;//列头筛选图标
            gpnl.EnableRowFiltering = false;//筛选行行头不可用
            gpnl.ImmediateResize = false;
            gpnl.EnableCellMerging = false;//合并单元格
            gpnl.GroupHeaderClickBehavior = GroupHeaderClickBehavior.ExpandCollapse;//分组单击行为
            gpnl.ColumnHeaderClickBehavior = ColumnHeaderClickBehavior.Select;//列头单击行为
            gpnl.EnableFiltering = true;
            gpnl.EnableColumnFiltering = true;
            gpnl.Filter.Visible = true;
            gpnl.FilterMatchType = FilterMatchType.RegularExpressions;
            gpnl.Filter.RowHeight = 25;
            gpnl.RowHeaderWidth = 50;
            gpnl.AllowRowHeaderResize = true;
            gpnl.TopLeftHeaderSelectBehavior = TopLeftHeaderSelectBehavior.NoSelection;

            gpnl.AllowRowResize = true;//允许用户拖动行高

            gpnl.DefaultVisualStyles.CellStyles.Default.AllowWrap = Tbool.True;//可自动换行
            gpnl.DefaultVisualStyles.CellStyles.Selected.AllowWrap = Tbool.True;
            gpnl.DefaultVisualStyles.CellStyles.Default.Font = new System.Drawing.Font("微软雅黑", 9);
            gpnl.ShowGroupUnderline = false;//是否显示分组下划线

            
            SPG.PrimaryGrid.ColumnAutoSizeMode = ColumnAutoSizeMode.AllCells;
            GridColumn col = new GridColumn();
            col = new GridColumn();
            col.Name = "Q_Name";
            col.HeaderText = "姓名";
            col.EditorType = typeof(GridLabelXEditControl);
            col.CellMergeMode = CellMergeMode.Horizontal;
            col.AutoSizeMode = ColumnAutoSizeMode.AllCells;
            SPG.PrimaryGrid.Columns.Add(col);
            col = new GridColumn();
            col.Name = "Q_Code";
            col.HeaderText = "QQ号码";
            col.EditorType = typeof(GridLabelXEditControl);
            //  col.FilterMatchType = FilterMatchType.RegularExpressions;
            col.CellMergeMode = CellMergeMode.None;
            col.AutoSizeMode = ColumnAutoSizeMode.Fill;
            SPG.PrimaryGrid.Columns.Add(col);
            col = new GridColumn();
            col.Name = "DateTime";
            col.HeaderText = "保存时间";
            col.EditorType = typeof(GridLabelXEditControl);
            //  col.FilterMatchType = FilterMatchType.RegularExpressions;
            col.CellMergeMode = CellMergeMode.None;
            col.AutoSizeMode = ColumnAutoSizeMode.AllCells;
            SPG.PrimaryGrid.Columns.Add(col);
            col = new GridColumn();
            col.Name = "id";
            col.HeaderText = "id";
            col.EditorType = typeof(GridLabelXEditControl);
            //  col.FilterMatchType = FilterMatchType.RegularExpressions;
            col.CellMergeMode = CellMergeMode.None;
            col.AutoSizeMode = ColumnAutoSizeMode.AllCells;
            
            SPG.PrimaryGrid.Columns.Add(col);
        }
        /// <summary>
        /// 删除
        /// </summary>
        public  void Del()
        {
            DB_ConfigEntities2 DBC = new DB_ConfigEntities2();
            QQ_Mess user = new QQ_Mess();
            user.id = int.Parse(TID.Text);
            DBC.Entry<QQ_Mess>(user).State = System.Data.Entity.EntityState.Deleted;
            DBC.SaveChanges();

        }
        /// <summary>
        /// 查询
        /// </summary>
        public void EFSelectAll()
        {
            if(cb.Checked ==true)
            {
                DB_ConfigEntities2 DBC = new DB_ConfigEntities2();
                SPG.PrimaryGrid.DataSource = DBC.QQ_Mess.ToList();
            }else
            {
                QQ_Mess Qm = new QQ_Mess();
                DB_ConfigEntities2 DBC = new DB_ConfigEntities2();
                SPG.PrimaryGrid.DataSource = DBC.QQ_Mess.Where(u => u.Q_Name ==TTTT_Name.Text).ToList();
            }
          

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cb.Checked = true;
            StyleSPG();
            EFSelectAll();
        }
        /// <summary>
        /// 添加
        /// </summary>
        public void EFadd()
        {
            DB_ConfigEntities2 DBC = new DB_ConfigEntities2();
            QQ_Mess QM = new QQ_Mess();
            QM.DateTime = DateTime.Parse( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            QM.Q_Code = T_QQ.Text;
            QM.Q_Name = T_Name.Text;
            DBC.QQ_Mess.Add(QM);
            DBC.SaveChanges();
        }


        public void EFupdate()
        {
            DB_ConfigEntities2 DBC = new DB_ConfigEntities2();
            QQ_Mess QM = new QQ_Mess();
            QM.DateTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            QM.id = int.Parse(T1.Text.ToString());
            QM.Q_Code = T3.Text.Trim();
            QM.Q_Name = T2.Text.Trim();
            DBC.Entry(QM).State = System.Data.Entity.EntityState.Modified;
            DBC.SaveChanges();

        }
        private void btadd_Click(object sender, EventArgs e)
        {
            EFadd();
            EFSelectAll();
        }

        private void btselect_Click(object sender, EventArgs e)
        {
            EFSelectAll();
        }

        private void btdel_Click(object sender, EventArgs e)
        {
            Del();
            EFSelectAll();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            EFupdate();
            EFSelectAll();
        }
    }
}

总结:新增

  删除  注意:id为主键,不唯一

修改 id为主键,不唯一

 

查询  

整表查询

条件查询

主键查询

 

转载于:https://www.cnblogs.com/hanke123/p/10869859.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值