EF方式增、删、改、查(基本使用)

  右击项目——添加——新建项——数据(C#)——选择ADO.NET实体数据模型——点击添加——然后根据实体数据模型向导来一步步的做。

用到的表

 1 using System;
 2 using System.Data;
 3 using System.Linq;
 4 using System.Windows.Forms;
 5 
 6 namespace WindowsFormsApp1
 7 {
 8     public partial class Form1 : Form
 9     {
10         public Form1()
11         {
12             InitializeComponent();
13         }
14 
15         private void simpleButton1_Click(object sender, EventArgs e)
16         {
17             XtraForm1 xtraForm1 = new XtraForm1();
18             xtraForm1.ShowDialog();
19         }
20         //查询
21         private void simpleButton2_Click(object sender, EventArgs e)
22         {
23             using (NorthwindEntities db = new NorthwindEntities())
24             {
25                 var cust = from c in db.Region select c;
26                 gridControl1.DataSource = cust.ToList();
27             }
28         }
29         //增加
30         private void simpleButton1_Click_1(object sender, EventArgs e)
31         {
32             using (NorthwindEntities db = new NorthwindEntities())
33             {
34                 Region region = new Region();
35                 region.RegionID =int.Parse(textEdit1.Text.Trim());
36                 region.RegionDescription = textEdit2.Text.Trim();
37                 db.Region.Add(region);
38                 int rows = db.SaveChanges();
39                 MessageBox.Show(rows.ToString());
40             }
41         }
42         //修改
43         private void simpleButton3_Click(object sender, EventArgs e)
44         {
45             using (NorthwindEntities db = new NorthwindEntities())
46             {
47                 int id = Convert.ToInt32(textEdit1.Text);
48                 Region region = db.Region.Where(i => i.RegionID == id).FirstOrDefault();
49                 if (region != null)
50                 {
51                     region.RegionDescription = textEdit2.Text;
52                     int rows = db.SaveChanges();
53                     MessageBox.Show(rows.ToString());
54                 }
55             }
56         }
57         //删除
58         private void simpleButton4_Click(object sender, EventArgs e)
59         {
60             using (NorthwindEntities db = new NorthwindEntities())
61             {
62                 int id = Convert.ToInt32(textEdit1.Text);
63                 Region region = db.Region.Where(i => i.RegionID == id).FirstOrDefault();
64                 if (region != null)
65                 {
66                     db.Region.Remove(region);
67                     int rows = db.SaveChanges();
68                     MessageBox.Show(rows.ToString());
69                 }
70             }
71         }
72         //单元格取值
73         private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
74         {
75             var index = gridView1.GetFocusedDataSourceRowIndex();
76             textEdit1.Text = gridView1.GetRowCellValue(index, "RegionID").ToString().Trim();
77             textEdit2.Text = gridView1.GetRowCellValue(index, "RegionDescription").ToString().Trim();
78         }
79     }
80 }
View Code

补充:批量增加数据

 1         //批量增加数据
 2         public void AddRange()
 3         {
 4             using (Northwind db = new Northwind())
 5             {
 6                 List<Region> regions = new List<Region>();
 7                 regions.Add(new Region { RegionID = 6, RegionDescription = "济南" });
 8                 regions.Add(new Region { RegionID = 7, RegionDescription = "合肥" });
 9                 regions.Add(new Region { RegionID = 8, RegionDescription = "天津" });
10                 db.Region.AddRange(regions);
11                 Response.Write(db.SaveChanges());
12             }
13         }
View Code

界面

posted @ 2019-11-27 15:22  liessay  阅读(443)  评论(0编辑  收藏  举报