There is a table with a column "Date" format 2018-03 in sql server. Entered year 2018 in textBox. How to display all records in a DataGridView with such a year?
2 answers
table with a column "Date" format 2018-03
This is not a "date". This is a regular line.
Entered in textBox year 2018
If you need to search for individual components and the “day” component is not foreseen, then break this field into two integer “year” and “month”
- Date type in the table - ne0n
- one@ ne0n Date type can not contain a day and can not be in some kind of "format" - Anton Shchyrov
|
private void butFound_Click(object sender, EventArgs e) { //поиск по году if(numericUpDownMonth.Value == 0) { SqlConnection conn = new SqlConnection(MainWindow.connectionString); conn.Open(); SqlCommand cmd = new SqlCommand(string.Format("Select * From dbo.[{0}] Where YEAR(date) = {1}", MainWindow.currentDate, numericUpDownYear.Value), conn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); dgvArchive.DataSource = dt; conn.Close(); } else {//поиск по году и месяцу SqlConnection conn = new SqlConnection(MainWindow.connectionString); conn.Open(); SqlCommand cmd = new SqlCommand(string.Format("Select * From dbo.[{0}] Where YEAR(date) = {1} and Month(date) = {2}", MainWindow.currentDate, numericUpDownYear.Value, numericUpDownMonth.Value), conn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); dgvArchive.DataSource = dt; conn.Close(); } } |
string.Format- great forstring.Formatwith dates. - nick_n_a pm