A
A
Alexey2013-11-13 22:56:39
SQL
Alexey, 2013-11-13 22:56:39

Passing DataTimePicker value to SQL query in C#?

Hello.
There is a Windows Forms project in C #, using an Access database. Different sorting works with standard built-in queries and output

this.hotelTableAdapter.FillByLonger(this.hoteldbDataSet.hotel);

in this format.
The last thing left to do is sort by date. To do this, you need to pass the selected date to the DataTimePicker in the SQL query.
C#
is not sure here either
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    DateTime result = dateTimePicker1.Value;
    this.Text = result.ToString();
}

SQL
SELECT        id, [ФИО проживающего], [Дата заселения], [Дата выселения], [Гостиничный номер]
FROM            hotel
WHERE        ([Дата выселения] LIKE '" + dateTimePicker1.Text"')

Question. How to properly sort by a given date? Is the parameter being passed correctly? Swears on To_DATE () in request. How to do this, has not yet decided. I just found out that the format for the request should be something like this #13/11/2013#

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
deleted-sh_fi5t, 2013-11-14
@Deia

You are asking for sorting and at the same time passing the date to "WHERE" ... If I understand you correctly, then you need the following:


var sqlCmd = new SqlCommand(@"SELECT 	id, 
          [ФИО проживающего], 
          [Дата заселения], 
          [Дата выселения], 
          [Гостиничный номер]
FROM			hotel
WHERE			([Дата выселения] LIKE '@dpDate')"");

sqlCmd.Parameters.Add("@dpDate", SqlDbType.DateTime);
sqlCmd.Parameters["@dpDate"] = dateTimePicker1.Value;

D
deleted-sh_fi5t, 2013-11-14
@deleted-sh_fi5t

deleted

T
Teacher, 2013-11-14
@Teacher

Good afternoon. And, if I may, in addition to the previous tip, it is better to store the date in the date format, not the string. You have a column [Date of eviction], isn't it a string?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question