Here are some save function for some situations:
Yes/No
/// <summary> /// /// </summary> /// <param name="ds"></param> public void SaveDataSet(DataSet ds, string parent, string child) { string message = "Do you want to save the data"; string caption = "Save Data"; if (ds.HasChanges()) { DialogResult result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { //DataSet dsChanged = ds.GetChanges(); DataTable tblParent = null; DataTable tblChild = null; if (!string.IsNullOrEmpty(parent)) { tblParent = ds.Tables[parent].GetChanges(); //error if table not existing } if (!string.IsNullOrEmpty(child)) { tblParent = ds.Tables[child].GetChanges(); //error if table not existing } DataSet ds1 = new DataSet(); if (tblParent != null) ds1.Tables.Add(tblParent); DataSet ds2 = new DataSet(); if (tblChild != null) ds2.Tables.Add(tblChild); //call save method(tblParent, tblChild, null); ds.AcceptChanges(); } catch (Exception ex) { //Error.ErrProc(ex); MessageBox.Show("Error"); ds.RejectChanges(); return; } } } else { MessageBox.Show("No Data Saved."); ds.RejectChanges(); } }
Yes/No/Cancel
public void SaveDataSetWithCancel(DataSet ds, string parent, string child) { string message = "Do you want to save the data"; string caption = "Save Data"; if (ds.HasChanges()) { DialogResult result = MessageBox.Show(message, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { //DataSet dsChanged = ds.GetChanges(); DataTable tblParent = null; DataTable tblChild = null; if (!string.IsNullOrEmpty(parent)) { tblParent = ds.Tables[parent].GetChanges(); //error if table not existing } if (!string.IsNullOrEmpty(child)) { tblParent = ds.Tables[child].GetChanges(); //error if table not existing } DataSet ds1 = new DataSet(); if (tblParent != null) ds1.Tables.Add(tblParent); DataSet ds2 = new DataSet(); if (tblChild != null) ds2.Tables.Add(tblChild); //call save method(tblParent, tblChild, null); ds.AcceptChanges(); MessageBox.Show("Data Saved."); } catch (Exception ex) { //Error.ErrProc(ex); MessageBox.Show("Error, try again"); return; } } else if (result == DialogResult.No) { MessageBox.Show("your data modified is discarded. it is not saved."); ds.RejectChanges(); return; } else { MessageBox.Show("your data is not saved. but it is still here."); return; } } else { MessageBox.Show("No new Data Saved."); ds.RejectChanges(); } }
SaveData Functions,布布扣,bubuko.com
时间: 2024-10-28 19:54:50