Subscribe to C# codes        RSS Feed
-----

Delete all records between specific dates

Icon 1 Comments
Ever needed to delete a series of records between dates? For example, maybe you have this problem? Or some problem where you need to go into a dataset and delete records between a series. Well I've got just the function for you:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // We must setup two DateTime objects...  I arbitrarily decided I wanted them both in the same array
            DateTime[] dateRange = new DateTime[2];
            dateRange[0] = new DateTime(2011,1,1);
            dateRange[1] = new DateTime(2011,1,20);


            // this GetDateRange function will return a list of each day between the two dates passed in as parameters
            // eg.   2011,1,1   
            //       2011,1,2
            //       2011,1,3
            //       ...
            //       2011,1,20
            List<DateTime> reportRange = GetDateRange(dateRange[0], dateRange[1]);

            //Swell!  They're even in date time format!

            foreach (DateTime date in reportRange)
            {
                MessageBox.Show("Rplace with payload.  Proof: " + date.ToShortDateString());
            }

        }



        /// <summary>
        /// Returns a list of dates beginning with StartingDate and ending with EndingDate
        /// Returns null if you got the order backwords (doh!)  =)
        /// </summary>
        /// <param name="StartingDate"></param>
        /// <param name="EndingDate"></param>
        /// <returns></returns>
        private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
        {
            if (StartingDate > EndingDate)
                return null;   // negitave numbers do not exist!  =P

            List<DateTime> dates = new List<DateTime>();
            DateTime tmpDate = StartingDate;

            do // atleast once
            {
                dates.Add(tmpDate);  // add each date to our list
                tmpDate = tmpDate.AddDays(1); // i++
            } while (tmpDate <= EndingDate);    // and until EndingDate of course

            return dates;
        }
    }
}

1 Comments On This Entry

Page 1 of 1

Limetree

07 June 2011 - 03:13 AM
Thanks for your code. That will really help to all the needy. Actually, removing unneeded data based on certain condition is, in fact, a good housekeeping strategy that all users of a database should know about.

--------------------------------------
Web Design Quote
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

September 2014

S M T W T F S
 123456
78910111213
14151617181920
21 222324252627
282930