1

I need to search some data in mysql record regarding date values using C# code. I am using following code in c# but i am unable find records. Please help me.

My C# code:

Select distinct v.* from Ven v inner join Des d on v.venid=d.venid and cast( d.despdate as datetime) between  cast('" + dTime1.Value.ToString("dd-MMM-yyy") + "' as datetime) and cast('" + dTime2.Value.ToString("dd-MMM-yyy") + "'as datetime)

My Mysql table record:

enter image description here

2
  • looks like the rdbms is mssql.. Commented Feb 19, 2013 at 7:59
  • @IswantoSan ya i am converting from ms-sql code to my-sql. dono how to write format query in my-sql to this query Commented Feb 19, 2013 at 8:10

3 Answers 3

2

You're going wrong by trying to include a value directly in your SQL query to start with. Use parameterized SQL and specify the value as a DateTime, and assuming your database table is also using a date/datetime field (which it should be), you should be fine.

You should avoid including values directly in your SQL for three reasons:

  • You'll end up with troublesome string conversions which can use inappropriate formats etc. That's the problem here.
  • You're inviting SQL injection attacks
  • You're mixing code and data, which makes it harder to read the code and harder to check the data

You want something like:

string sql = @"Select distinct v.* from Ven v 
               inner join Des d on v.venid=d.venid 
               and cast(d.despdate as datetime) between @start and @end";
using (MySqlCommand command = new MySqlCommand(connection, sql))
{
    command.Parameters.AddWithValue("@start", startDateTime);
    command.Parameters.AddWithValue("@end", endDateTime);
    // Execute the command here
}

If Des.despdate isn't already a suitable data type, you should change that...

Sign up to request clarification or add additional context in comments.

Comments

1

convert your column d.despdate into DATE using STR_TO_DATE

SELECT  ...
FROM    ...
WHERE   STR_TO_DATE(d.despdate, '%d-%b-%Y') BETWEEN '2013-01-15' AND '2013-01-30'

for better performance, parameterized the query using MySQLCommand object. ex,

string connStr = "connection string here";
string query = @"SELECT ... 
                FROM... 
                WHERE STR_TO_DATE(d.despdate, '%d-%b-%Y') BETWEEN @date1 AND @date2"
using(MySqlConnection _conn = new MySqlConnection(connStr))
{
    using (MySqlCommand comm = new MySqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = query;
        comm.Parameters.AddWithValue("@date1", dTime1.Value);
        comm.Parameters.AddWithValue("@date2", dTime2.Value);
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
        }
        catch(MySqlException e)
        {
            // do something with
            // e.ToString()
        }
    }
}

Comments

0

It should be yyyy

 Select distinct v.* from Ven v inner join Des d on v.venid=d.venid and cast( d.despdate as datetime) between  cast('" + dTime1.Value.ToString("dd-MMM-yyyy") + "' as datetime) and cast('" + dTime2.Value.ToString("dd-MMM-yyyy") + "'as datetime)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.