首页 > 其他 > 详细

SaveData Functions

时间:2014-08-09 21:02:49      阅读:290      评论:0      收藏:0      [点我收藏+]

Here are some save function for some situations:

 

Yes/No

bubuko.com,布布扣
/// <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();
            }

        }
View Code


Yes/No/Cancel

bubuko.com,布布扣
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();
            }

        }
View Code

 

SaveData Functions,布布扣,bubuko.com

SaveData Functions

原文:http://www.cnblogs.com/kevinygq/p/3901444.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!