使用月从数据库获取数据

本文关键字:获取 数据 数据库 | 更新日期: 2023-09-27 18:17:54

try
{
    conn5.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = conn5;
    string query = "select * from OrderDataListTable WHERE MONTH(`DATETIME`) = dateString";
    command.CommandText = query;
    reader = command.ExecuteReader();

将输入月份与数据库进行比较的正确语法是什么?

String dateString = Convert.ToString(textBox2.Text);
DateTime month = Convert.ToDateTime(dateString + "01, 1990").Month;

使用月从数据库获取数据

对于间隔的日期,您可以使用如下格式的字符串表达式:

DateTime monthDate = DateTime.Parse(textBox2.Text);
string startDate = new DateTime(monthDate.Year, monthDate.Month, 1).ToString("yyyy'/'MM'/'dd");
string endDate = new DateTime(monthDate.Year, monthDate.Month + 1, 1).AddDays(-1).ToString("yyyy'/'MM'/'dd");

然后:

string query = "select * from OrderDataListTable where [DATETIME] between #" + startDate + "# and #" + endDate + "#";