Windows Forms: Insert Update Delete View data in SQL Server using 3 Tiers in C#
By FoxLearn 6/25/2017 7:54:21 PM 6.42K
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "EF3Tiers" and then click OK
Select Class Library, then create DataLayer, Model and BusinessLayer project
Step 2: Design your form as below
Form1
frmAddEditContact
Step 3: Create an EF model as below
You should create a Contact table with fileds corespond the contact class
Step 4: Add an interface to DataLayer project
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; namespace DataLayer { public interface IContactRepository { List<Contact> GetAll(); Contact GetById(int id); Contact Insert(Contact obj); void Update(Contact obj); void Delete(Contact obj); } }
Create a ContactRepository class to handle access database
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; namespace DataLayer { public class ContactRepository : IContactRepository { public void Delete(Contact obj) { using(ContactEntities db = new ContactEntities()) { db.Contacts.Attach(obj); db.Contacts.Remove(obj); db.SaveChanges(); } } public List<Contact> GetAll() { using (ContactEntities db = new ContactEntities()) { return db.Contacts.ToList(); } } public Contact GetById(int id) { using (ContactEntities db = new ContactEntities()) { return db.Contacts.Find(id); } } public Contact Insert(Contact obj) { using (ContactEntities db = new ContactEntities()) { db.Contacts.Add(obj); db.SaveChanges(); return obj; } } public void Update(Contact obj) { using (ContactEntities db = new ContactEntities()) { db.Contacts.Attach(obj); db.Entry(obj).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } } }
Step 5: Add a ContactServices to BusinessLayer project
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DataLayer; using Model; namespace BusinessLayer { public static class ContactServices { static IContactRepository repository; static ContactServices() { repository = new ContactRepository(); } public static List<Contact> GetAll() { return repository.GetAll(); } public static Contact GetById(int id) { return repository.GetById(id); } public static Contact Insert(Contact obj) { return repository.Insert(obj); } public static void Update(Contact obj) { repository.Update(obj); } public static void Delete(Contact obj) { repository.Delete(obj); } } }
You can easily change code in business layer, don't need modify code in UI
Step 6: Add code to handle your form as below
Form1
using BusinessLayer; using Model; 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 EF3Tiers { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Call business layer to get data contactBindingSource.DataSource = ContactServices.GetAll(); } private void btnAdd_Click(object sender, EventArgs e) { using(frmAddEditContact frm =new frmAddEditContact(null)) { if (frm.ShowDialog() == DialogResult.OK) contactBindingSource.DataSource = ContactServices.GetAll(); } } private void btnEdit_Click(object sender, EventArgs e) { if (contactBindingSource.Current == null) return; using (frmAddEditContact frm = new frmAddEditContact(contactBindingSource.Current as Contact)) { if (frm.ShowDialog() == DialogResult.OK) contactBindingSource.DataSource = ContactServices.GetAll(); } } private void btnDelete_Click(object sender, EventArgs e) { if (contactBindingSource.Current == null) return; if(MessageBox.Show("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ContactServices.Delete(contactBindingSource.Current as Contact); contactBindingSource.RemoveCurrent(); } } } }
frmAddEditContact
using BusinessLayer; using Model; 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 EF3Tiers { public partial class frmAddEditContact : Form { bool IsNew; public frmAddEditContact(Contact obj) { InitializeComponent(); if (obj == null) { contactBindingSource.DataSource = new Contact(); IsNew = true; } else { contactBindingSource.DataSource = obj; IsNew = false; } } private void frmAddEditContact_FormClosing(object sender, FormClosingEventArgs e) { if (DialogResult == DialogResult.OK) { if (string.IsNullOrEmpty(txtContactName.Text)) { MessageBox.Show("Please enter your contact name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); txtContactName.Focus(); e.Cancel = true; return; } if (IsNew) ContactServices.Insert(contactBindingSource.Current as Contact); else ContactServices.Update(contactBindingSource.Current as Contact); } } } }
VIDEO TUTORIALS
- Insert Update Delete and View data from SQL Database using ORM Lite in C#
- How to Insert Update Delete Search Records in C#
- How to Insert Update Delete From Database in VB.NET
- How to Insert Update Delete and View data from SQL Database in C# using ADO.NET
- How to Insert Update Delete View data from SQL Database in C#
- Windows Forms: Insert Update Delete data in Database from DataGridView in C#
- Windows Forms: Insert Update Delete Search data from local database in C#
- Windows Forms: Insert Update Delete Select in SQL Server in C#