如何使用 C# 在 SQL 服务器数据库中将 mm/dd/yyyy 与月份进行比较
How to compare mm/dd/yyyy with month only in SQL Server database using C#
我的数据以这种格式保存 mm/dd/yyyy
...我只想将我的数据与月份进行比较....我的意思是我想显示与 datetimepicker 选择的那个月份相关的所有数据.
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Coregen\documents\visual studio 2013\Projects\piechart\piechart\sale.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Tab_n WHERE Date='" + this.dateTimePicker1.Value + "'"; //
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
con.Close();
您可以按照以下方式使用:
cmd.CommandText = "SELECT Column_1, Column_2 FROM Tab_n WHERE DATEPART(Month,Date) ='" + this.dateTimePicker1.Value.Month + "'";
您可以使用 SQLParameter
简化此操作:
cmd.Parameters.AddWithValue("@Date_Of_Something",this.dateTimePicker1.Value.Month);
编辑:
由于您也想比较年份,因此您必须添加另一个 where
条件。这将再次使用 DATEPART
函数。
由于我认为您现在一定已经采纳了使用参数化查询的建议,因此在回答以下附录时考虑了该方法。
cmd.CommandText = "SELECT Column_1, Column_2 FROM Tab_n WHERE DATEPART(Month,Date) = @DatePicker_Month AND DATEPART(Year,Date) = @DatePicker_Year";
在这种情况下,您将有两个参数:
cmd.Parameters.AddWithValue("@DatePicker_Month",this.dateTimePicker1.Value.Month);
cmd.Parameters.AddWithValue("@DatePicker_Year",this.dateTimePicker1.Value.Year);
例如,这将允许您获取第 5 个月的结果(由 datepart_month 变量指定)2016 年(由 datepart_year 变量指定 ).
希望对您有所帮助!!!
我认为在您的查询中使用 BETWEEN
是最佳解决方案。首先找到月份的开始和结束日期,然后将它们放入 BETWEEN
operator:
// get DatePicker selected year and month
int year = dateTimePicker1.Value.Year;
int month = dateTimePicker1.Value.Month;
// start from the first day of the month
int startDay = 1;
// find the last day of selected month
int endDay = DateTime.DaysInMonth(year, month);
DateTime startDate = new DateTime(year, month, startDay);
DateTime endDate = new DateTime(year, month, endDay);
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Coregen\documents\visual studio 2013\Projects\piechart\piechart\sale.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Tab_n WHERE Date BETWEEN @startDate AND @endDate";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@startDate", startDate);
cmd.Parameters.AddWithValue("@endDate", endDate);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
con.Close();
我的数据以这种格式保存 mm/dd/yyyy
...我只想将我的数据与月份进行比较....我的意思是我想显示与 datetimepicker 选择的那个月份相关的所有数据.
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Coregen\documents\visual studio 2013\Projects\piechart\piechart\sale.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Tab_n WHERE Date='" + this.dateTimePicker1.Value + "'"; //
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
con.Close();
您可以按照以下方式使用:
cmd.CommandText = "SELECT Column_1, Column_2 FROM Tab_n WHERE DATEPART(Month,Date) ='" + this.dateTimePicker1.Value.Month + "'";
您可以使用 SQLParameter
简化此操作:
cmd.Parameters.AddWithValue("@Date_Of_Something",this.dateTimePicker1.Value.Month);
编辑:
由于您也想比较年份,因此您必须添加另一个 where
条件。这将再次使用 DATEPART
函数。
由于我认为您现在一定已经采纳了使用参数化查询的建议,因此在回答以下附录时考虑了该方法。
cmd.CommandText = "SELECT Column_1, Column_2 FROM Tab_n WHERE DATEPART(Month,Date) = @DatePicker_Month AND DATEPART(Year,Date) = @DatePicker_Year";
在这种情况下,您将有两个参数:
cmd.Parameters.AddWithValue("@DatePicker_Month",this.dateTimePicker1.Value.Month);
cmd.Parameters.AddWithValue("@DatePicker_Year",this.dateTimePicker1.Value.Year);
例如,这将允许您获取第 5 个月的结果(由 datepart_month 变量指定)2016 年(由 datepart_year 变量指定 ).
希望对您有所帮助!!!
我认为在您的查询中使用 BETWEEN
是最佳解决方案。首先找到月份的开始和结束日期,然后将它们放入 BETWEEN
operator:
// get DatePicker selected year and month
int year = dateTimePicker1.Value.Year;
int month = dateTimePicker1.Value.Month;
// start from the first day of the month
int startDay = 1;
// find the last day of selected month
int endDay = DateTime.DaysInMonth(year, month);
DateTime startDate = new DateTime(year, month, startDay);
DateTime endDate = new DateTime(year, month, endDay);
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Coregen\documents\visual studio 2013\Projects\piechart\piechart\sale.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Tab_n WHERE Date BETWEEN @startDate AND @endDate";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@startDate", startDate);
cmd.Parameters.AddWithValue("@endDate", endDate);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
con.Close();