使用月份从数据库获取数据

时间:2015-10-16 03:50:57

标签: c# ms-access

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;

1 个答案:

答案 0 :(得分:0)

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

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 + "#";