c# - 如何在带有复选框的列表框中显示 1 个月的数据库
问题描述
所以我能够将每日数据显示到列表框中,但是如果我使用一个复选框,它将显示 1 个月的数据,我该怎么办?
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
//label1.Text = monthCalendar1.SelectionStart.ToString();
lstres.Items.Clear();
con.Open();
string date = monthCalendar1.SelectionStart.ToString();
SqlCommand cmd = new SqlCommand("Select * from reservation where Date = '" + date + "'", con);
//sqlcommand cmd= to choose all the data from database where date is from Date
DataTable dt = new DataTable();
// datatable = to represents data table
SqlDataAdapter da = new SqlDataAdapter(cmd);
//sqldataadapter = to link database connection
da.Fill(dt);
//DateTime dateAndTime = DateTime.Now;
// Will give you smth like 25/05/2011
//lstres.Text = dateAndTime.ToString("dd/MM/yyyy");
foreach (DataRow dr in dt.Rows)
//foreach to transfer items in an array and display it
{
string dateonly = Convert.ToDateTime(dr["Date"]).ToString("dd/MM/yyyy");
string show = dateonly + " " + dr["TimeStart"].ToString() +
" " + "-" + " " + dr["TimeEnd"].ToString() + " " + dr["RoomType"].ToString();
lstres.Items.Add(show);
}
con.Close();
}
这段代码是每天的,但如何为每月做任何想法?
解决方案
要得到整个月,你需要计算当月的第一天,以及下个月的第一天,你可以这样做:
DateTime selectedDate = monthCalendar1.SelectionStart;
DateTime fromDate = date.AddDays(-date.Day + 1);
DateTime toDate = fromDate.AddMonths(1);
然后,您可以将它们种植在 sql 命令中,如下所示:
SqlCommand cmd = new SqlCommand($"Select * from reservation where Date >= '{fromDate:yyyy-MM-dd}' AND Date < '{toDate:yyyy-MM-dd}'", con);
推荐阅读
- python - 检查值对是否满足熊猫中的布尔值
- javascript - 单击按钮时无法打开新窗口
- java - 为什么这个 Future 的方法阻塞了主线程?
- php - 如何使用控制台命令中的参数调用控制器中的方法
- javascript - 选项卡内的事件范围
- linux - 如何将所有合并的远程 git 分支转换为标签?(创建标签,删除分支)
- python - Python:从包含特定单词的文本中选择一个子句
- xml - 使用应用模板时只选择祖父母下的第一个值
- apache-kafka - 为什么尝试查看偏移量时kafka会显示错误?
- selenium - 如何使用 ChromeDriver Chrome 和 Selenium 摆脱 BrowserStack Local 扩展弹出窗口