Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete data from database which are older than 1 or n year(user will enter year),
how to do this plz help me.
thanks in advance.
sushil

What I have tried:

I search on google but i dint get any solution, I dont know how to do this,
Posted
Updated 3-Jul-16 6:24am

1 solution

Try:
C#
int year;
if (!int.TryParse(userInputTextBox.Text, out year))
   {
   // Report problem to user
   ...
   return;
   }
DateTime olderThan = DateTime.Now.Date.AddYears(-year);
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("DELETE FROM myTable WHERE dateColumn < @LIM", con))
        {
        cmd.Parameters.AddWithValue("@LIM", olderThan);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
Member 11859517 3-Jul-16 12:53pm    
thanx,

actualy i want to delete the images.
Member 11859517 3-Jul-16 12:56pm    
DateTime olderThan = DateTime.Now.Date.AddYears(-year);
what it means (-year) i dint get.
OriginalGriff 3-Jul-16 13:21pm    
What happens if you add a negative number?
int x =42;
x += -3;
Console.WriteLine(x);

Try it and see...
Member 11859517 3-Jul-16 23:50pm    
thanks i got it.
OriginalGriff 4-Jul-16 5:03am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900