PetaPoco 更新只是修改了 winform 中的记录 [英] PetaPoco update just modified records in winforms

查看:17
本文介绍了PetaPoco 更新只是修改了 winform 中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

namespace PetaPocoTest
{
    public partial class Form1 : Form
    {
        PetaPoco.Database db = new PetaPoco.Database("PgConnection");

        IEnumerable<customers> allCustomers;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            allCustomers = db.Query<customers>("SELECT * FROM customers");
            mGrid.DataSource = allCustomers .ToList();            
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
             foreach (var a in allCustomers)
             {
               db.Save("customers", "custumer_id", a);
             }
        }
    }
}

bat 这会更新所有记录,无论它们是否更改.所以,我的问题是有人知道如何只更新 petapoco 中更改的记录吗?

bat this updates all record, no matter if they are changed or not. So, my question is does anyone know how to update only changed records in petapoco ?

推荐答案

我就是这样做的(最终!):

This is how I did it (eventually!) :

namespace PetaPocoTest
{
    public partial class Form1 : Form
    {
        PetaPoco.Database db = new PetaPoco.Database("PgConnection");
        Dictionary<string, int> modRows = new Dictionary<string, int>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindingSource = db.Fetch<customers>("SELECT * FROM customers");
            mGrid.DataSource = bindingSource;            
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
           db.BeginTransaction();

               foreach (customers c in bindingSource)
               {
                   if (modRows.ContainsKey(c.id.ToString()))
                   {
                       db.Save("customers", "id", c);
                   }
               }

           db.CompleteTransaction();
        }

        private void mGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int recId = ((customers)bindingSource.Current).id;

            if (!modRows.ContainsKey(recId.ToString()))
            {
               modRows.Add(recId.ToString(), recId);
            }
        }

    }
}

它的功能,但如果有人有更好的想法,请分享!

It functions, but if anyone has better idea pls share!

这篇关于PetaPoco 更新只是修改了 winform 中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆