我正在使用 SQLite 数据库。有必要从数据库中获取一定时间段的数据,比如 BETWEEN (MySQL) 的时间。但是有一个细微差别,在数据库中,时间由一组值存储:G.M.D.H.M.S. 不只是一行。
这是请求:
@$SELECT employeeId, year, month, day, hour, minute,
second FROM [employeers] WHERE date >= {startTime} AND date <= {endTime};
和代码块本身:
public List<EmployeeDto> SelectEmployeeBetweenTimeStamp(string startTime, string endTime)
{
var employeeDto = new List<EmployeeDto>();
using (var connection = new SqliteConnection(connectionString))
{
using (var command = new SqliteCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = @$SELECT id, employeeId, year, month, day, hour, minute, second, employeeName, employeeLastName FROM
[employeers] WHERE date >= {startTime} AND date <= {endTime};
command.CommandType = CommandType.Text;
var reader = command.ExecuteReader();
while (reader.Read())
{
breezeDto.Add(new EmployeeDto
{
Id = Convert.ToInt32(reader["id"]),
EmployeeId = Convert.ToInt32(reader["employeeId"]),
Year = Convert.ToInt32(reader["year"]),
Month = Convert.ToInt32(reader["month"]),
Day = Convert.ToInt32(reader["day"]),
Hour = Convert.ToInt32(reader["hour"]),
Minutes = Convert.ToInt32(reader["minute"]),
Seconds = Convert.ToInt32(reader["second"]),
EmployeeName = reader["employeeName"].ToString(),
EmployeeLastName = reader["employeeLastName"].ToString()
});
}
}
}
throw new Exception();
}
输入参数是 DateTime.ToString() 起点和终点。
如何将数据库中的日期收集为一个整体并在查询中进行比较?

坦率地说,将日期分成不同的列是一个糟糕的决定。
作为出路:在类中,
EmployeeDto添加一个属性public DateTime ResultDate => new DateTime(this.Year, this.Month, ...)从表中读取所有数据后,您可以使用 LINQ 使用此属性处理日期。
如果您只是在编写应用程序的初始阶段,最好更改表的结构以将日期存储在一列中,然后您可以立即在 SQL 中使用日期。
或者,您可以编写这样的查询,将数据库中的数据带到日期和时间
并与方法传入的参数进行比较。