PetaPoco更新了winforms中的修改记录

时间:2014-02-18 11:04:38

标签: c# winforms petapoco

我有这段代码:

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中已更改的记录?

2 个答案:

答案 0 :(得分:1)

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

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);
            }
        }

    }
}

它起作用,但如果有人有更好的想法请分享!

答案 1 :(得分:0)

这是我通过 Form_Load

db.BeginTransaction 执行此操作的最简单方法
 private void Form1_Load(object sender, EventArgs e)
 {
        studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
        db.BeginTransaction(); // Begin Transaction here
 }

更新 CellValueChanged )上只需执行 db.Save(),但尚未提交,因为我们的交易尚未完成

 private void studentDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        db.Save((student)studentBindingSource.Current);         
    }

是的,我们也可以插入删除

插入

private void buttonInsert_Click(object sender, EventArgs e)
{
    student newStudent = new student
    {
        StudentName = "POCONEW"
    }
    studentBindingSource.Add(newStudent);
    db.Save(newStudent);
}

删除

private void studentDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    int rowIndex = e.Row.Index; // We use Gridview row index instead because BindingSource.Current isn't work when user drag then deleting multiple row 
    db.Delete(studentDataGridView.Rows[rowIndex].DataBoundItem as student);
}

最后保存更改我们只做 db.CompleteTransaction() :在用户按下保存按钮之后,如果你没有关闭表格,你必须再次调用db.BeginTransaction(),如果没有用户编辑的所有内容将被自动保存,因为我们没有交易不再

private void btSave_Click(object sender, EventArgs e)
{
    db.CompleteTransaction();
    db.BeginTransaction(); // everything user edit after this will be saved automatically because we don't have Transaction anymore so we Begin it again
}