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