Samet Yılmaz
Samet Yılmaz

Reputation: 11

Building a SQL query to searching between values of 2 DateTimePickers in C#

How can I search in a database using datetimepicker1.text and datetimepicker2.text I wrote this:

"Select * from gelirler where borc_tarihi between" + dateTimePicker1.Text + "'+"'and" + dateTimePicker2.Text, baglanti);

but it did not work.

Upvotes: 0

Views: 52

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

It looks like you have a problem with the '' in the query. It should be

var query = "select * from gelirler 
             where borc_tarihi between '"+ dateTimePicker1.Text +"' and '"+ dateTimePicker2.Text +"'"

But I'd recommend you to look into parameterized queries all together.. To avoid sql injections.

using (SqlCommand command = 
    new SqlCommand("select * from gelirler where borc_tarihi between @begin_time and @end_time", connection))
{
    command.Parameters.Add("@begin_time", SqlDbType.Datetime);
    command.Parameters["@begin_time"].Value = datetimePicker1.Text;

    command.Parameters.Add("@end_time", SqlDbType.Datetime);
    command.Parameters["@end_time"].Value = datetimePicker2.Text;
    /* execute the query... */
} 

Upvotes: 1

Related Questions