简体   繁体   中英

Getting values from datetimepicker wont work in SQL Server in C#

I need to create query based on date and time value from database .

In my SQL (I'm showing here only the relevant part) I have:

WHERE 
   [QueuePerformanceByPeriodStats].[MidnightStartDate] >= "+ dateTimePicker1.Value.ToString()+ "08:00:00.000"
   + " AND [QueuePerformanceByPeriodStats].[MidnightStartDate] <= "+ dateTimePicker1.Value.ToString() + "23:59:00.000"

And I'm using datetimepicker to send the value to this SQL statement:

private void Form1_Load(object sender, EventArgs e)
{
    dateTimePicker1.Value = DateTime.Now;
}

It doesn't work ! Any ideas ?

You shouldn't be using string concatenation to put the value into your query. You should be using SQLParameters (for various reasons you should always be in the habit of doing this including SQL injection attacks).

If for some reason that isn't possible, you need to encapsulate the value in single quotes and format it as yyyy-MM-dd for SQL Server to reliably get the right value for the date. You should probably also have a space before the time value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM