Question

I have this code :

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 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 ?

Was it helpful?

Solution

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!

OTHER TIPS

This is the simplest way from me to do this by db.BeginTransaction on Form_Load

 private void Form1_Load(object sender, EventArgs e)
 {
        studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
        db.BeginTransaction(); // Begin Transaction here
 }

On Update (CellValueChanged) just simply do db.Save() this doesn't commit yet because our transaction is not complete yet

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

And yes we can do Insert and Delete as well !!!

Insert

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

Delete

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

Finally to save change we just do db.CompleteTransaction() :Just a little note here after user press the Save Button if you don't close the form you have to call db.BeginTransaction() again if not everything user edit after this will be saved automatically because we don't have Transaction anymore

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top