Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have stored data in SQL.there is a column "date" with 'DateTime' data type. now i want to compare system date with my database date using query. please help friends thanks in advance.

i have tried this code which are shown in its shows the total data of table. i want to show data current date to last 3 month of data.(thanks)

What I have tried:

SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
con.Open();
try
{
DateTime maxdate = DateTime.Now.Date;
DateTime mindate = DateTime.Now.Date.AddMonths(-3);
SqlDataSource1.SelectCommand = "select * from general1 where date<='" + maxdate + "' AND date>='" + mindate + "'" ;
Posted
Updated 4-Nov-16 2:46am
Comments
[no name] 4-Nov-16 8:22am    
"i want to show data current date to last 3 month of data", okay.... and?
navi G 4-Nov-16 8:28am    
please see my code and tell me what is wrong with its.
[no name] 4-Nov-16 8:35am    
I see your code and I can't tell you what's wrong with it. I can't run your code, I don't have access to your config file and I don't have access to your database. It is up to YOU to tell us what you see on your screen, or describe a problem or even ASK A QUESTION!
Sinisa Hajnal 4-Nov-16 8:27am    
Use parametrized query and send UTC formatted data as parameters. Or use stored procedure and send datetime parameters directly. I commonly use UTC dates simply because I know what I'm sending and how I'm transforming it in the stored procedure (helps with compatibility)

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
So try:
C#
DateTime maxdate = DateTime.Now.Date;
DateTime mindate = DateTime.Now.Date.AddMonths(-3);
SqlDataSource1.SelectCommand = "SELECT * FROM general1 WHERE [date] BETWEEN @MND AND @MXD";
SqlDataSource1.SelectCommand.Parameters.AddWithValue("@MND", mindate);
SqlDataSource1.SelectCommand.Parameters.AddWithValue("@MXD", maxdate);
 
Share this answer
 
Something like this:

C#
SqlParameter[] parameters = new SqlParameter[]
{
    new SqlParameter("@minDate", minDate.Date),
    new SqlParameter("@maxDate", maxDate.Date)
};
string query = "select * from general1 where CONVERT(date, table.date) between @maxdate AND @mindate" ;
DataSet dataset = null;
using (SqlConnection conn = new SqlConnection(connectionString))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand(query, connetionstring))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddRange(parameters);
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            dataset = new DataSet();
            int rows = adapter.Fill(dataset);
        }
    }
}
 
Share this answer
 
v2

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



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