I am creating a database application using Visual C# Express and MySQL. The issue is that I only want to store data to the database from the C# forms if the data has actually been changed, the way I do it now is that when the ‘Save’ button is clicked all the all the fields in the form are saved into the database, whether or not they have been changed.
The solution I have come up with is just to check for every field in the form if it has been changed and then change the SQL command accordingly, i.e.
//when the details are loaded into the form
String strOriginalName = txtFirstname.Text;
//when the save button is checked
if(strOriginalName != txtFirstname.Text)
{
String strUpdate = "UPDATE table SET firstName = txtFirstname.Text";
MySqlCommand cmdUpdate = new MySqlCommand(strUpdate, connection);
cmdUpdate.ExecuteNonQuery();
}
Does anyone else have a better or alternative solution? Thank you for your help, if I was vague in any part, please let me know and I can try explain further.
I suggest to use some OR Mapper (NHibernate) for example. It will do it all for you. In addition this will give you caching and other cool things for free 🙂
Lessons learned for future are priceless and your next applications will be grow faster and faster. 🙂