首页 > 代码库 > SaveData Functions
SaveData Functions
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(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。