[DEVEXPRESS] Tutorial Insert, Update, Delete DB SQLSERVER with XPO Framework

[DEVEXPRESS] Tutorial Insert, Update, Delete DB SQLSERVER with XPO Framework
Đăng bởi: Thảo Meo - Lượt xem: 749 15:09:21, 02/11/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn thao thác thêm, xóa, sửa trên Devexpress sử dụng XPO Framework.

[DEVEXPRESS] Tutorial Insert, Update, Delete DB SQLSERVER with XPO Framework

Express Persistent Objects (or XPO) is an Object-Relational Mapping (ORM) tool for .NET.

XPO một công cụ của Devexpress, giống như Dapper hay Entity Framework.

Giao diện demo ứng dụng CRUD SQL with XPO:

xpo_thumb

Video hướng dẫn step by step từng bước thực hiện:

Source code tạo class Student.cs:

using DevExpress.Xpo;
using System;

namespace SimpleXPOCrud
{
    [OptimisticLocking(true)]
    public class Student : XPLiteObject
    {
        
        public Student(Session session) : base(session)
        {
            session.LockingOption = LockingOption.Optimistic;
        }

        string _code;
        [Key, Persistent("Code")]
        [Size(10)]
        public string Code
        {
            get { return _code; }
            set { SetPropertyValue(nameof(Code), ref _code, value); }
        }

        string _name;   
        [Size(50)]
        public string Name
        {
            get { return _name; }
            set { SetPropertyValue(nameof(Name), ref _name, value); }
        }

        string _address;
        [Size(50)]
        public string Address
        {
            get { return _address; }
            set { SetPropertyValue(nameof(Address), ref _address, value); }
        }

        DateTime _dateOfBirth;       
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { SetPropertyValue(nameof(DateOfBirth), ref _dateOfBirth, value); }
        }

        string _gender;
        [Size(50)]
        public string Gender
        {
            get { return _gender; }
            set { SetPropertyValue(nameof(Gender), ref _gender, value); }
        }


    }

}

Source code trên Form1.cs

using DevExpress.Xpo.DB;
using DevExpress.Xpo;
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;
using DevExpress.XtraEditors;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace SimpleXPOCrud
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            gridView1.FocusedRowChanged += GridView1_FocusedRowChanged;
        }

        private void GridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var student = gridView1.GetFocusedRow() as Student;
            if (student != null)
            {
                txtCode.Text = student.Code;
                txtFullName.Text = student.Name;
                txtAddress.Text = student.Address;
                txtDate.EditValue = student.DateOfBirth;
                txtGender.EditValue = student.Gender;
            }
        }

        private XPCollection xpCollection;
        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = MSSqlConnectionProvider.GetConnectionString("TMV2209068\\SQLEXPRESS", "sa", "LapTrinhVBNet@2022", "XPOCRUDDemo");
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
            LoadData();
        }

        public void LoadData() {
            xpCollection = new XPCollection(typeof(Student));
            gridControl1.DataSource = xpCollection;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var dlg = XtraMessageBox.Show($"Do you want <b><color=red>Delete</color></b> student selected?", "Mesasge", MessageBoxButtons.YesNo, MessageBoxIcon.Question, DevExpress.Utils.DefaultBoolean.True);
            if (dlg == DialogResult.Yes)
            {
                using (var uow = new UnitOfWork())
                {
                    var list = gridView1.GetSelectedRows().Select(x => gridView1.GetRow(x) as Student).ToList();

                    var students = uow.Query<Student>().AsEnumerable().Where(s => list.Any(x => s.Code == x.Code)).ToList();
                    uow.Delete(students);
                    uow.CommitChanges();
                    xpCollection.Reload();

                }
            }
               
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (var uow = new UnitOfWork())
            {
                var student = new Student(uow);
                student.Code = txtCode.Text;
                student.Name = txtFullName.Text;
                student.DateOfBirth = DateTime.Parse(txtDate.EditValue.ToString());
                student.Address = txtAddress.Text;
                student.Gender = txtGender.Text;

                var isExisted = uow.Query<Student>()
                                .Where(x => x.Code == txtCode.Text).ToList().Count > 0;
                if (isExisted)
                {
                    XtraMessageBox.Show($"Mã sinh viên: {student.Code} này đã tồn tại.");
                    txtCode.Focus();
                    return;
                }

                uow.CommitChanges();
                xpCollection.Reload();
                XtraMessageBox.Show($"Save Successful!", "Message");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var studentCode = txtCode.Text;

            var dlg = XtraMessageBox.Show($"Do you want update Student: <b><color=red>{txtFullName.Text}</color></b>", "Mesasge", MessageBoxButtons.YesNo, MessageBoxIcon.Question, DevExpress.Utils.DefaultBoolean.True);
            if (dlg == DialogResult.Yes)
            {
                using (var uow = new UnitOfWork())
                {
                    var student = uow.Query<Student>().Where(s => s.Code == studentCode).FirstOrDefault();
                    student.Address = txtAddress.Text;
                    student.Gender = txtGender.Text;
                    student.Name = txtFullName.Text;
                    student.DateOfBirth = DateTime.Parse(txtDate.EditValue.ToString());
                    uow.CommitChanges();
                    xpCollection.Reload();

                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtFullName.Text = "";
            txtAddress.Text = "";
            txtGender.Text = "";
            txtCode.Focus();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: xpo devexpress c#crud with xpo framework c#xpo devexpressxpo

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Tutorial Insert, Update, Delete DB SQLSERVER with XPO Framework
Đăng bởi: Thảo Meo - Lượt xem: 749 15:09:21, 02/11/2022DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.