码迷,mamicode.com
首页 > 其他好文 > 详细

SaveData Functions

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

标签:style   blog   http   color   os   io   for   art   

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

标签:style   blog   http   color   os   io   for   art   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!