Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将天重叠为一个“天”;“天”;_C#_Mysql_Timestamp - Fatal编程技术网

C# 如何将天重叠为一个“天”;“天”;

C# 如何将天重叠为一个“天”;“天”;,c#,mysql,timestamp,C#,Mysql,Timestamp,好吧,那么。我有一个数据库,它有一列时间戳。“一天”的开始时间是早上7点。比如说今天,它将从2012年6月25日的0700开始,到2012年6月26日的0700结束。我需要在24小时内进行计算。现在,我正在考虑做一个查询,从0700+24小时开始获取所有信息,但我不是100%地了解如何表达所述查询。谢谢你的帮助 SELECT your_needed_columns FROM your_table WHERE your_timestamp_column BETWEEN 'start_ti

好吧,那么。我有一个数据库,它有一列时间戳。“一天”的开始时间是早上7点。比如说今天,它将从2012年6月25日的0700开始,到2012年6月26日的0700结束。我需要在24小时内进行计算。现在,我正在考虑做一个查询,从0700+24小时开始获取所有信息,但我不是100%地了解如何表达所述查询。谢谢你的帮助

SELECT 
your_needed_columns 
FROM 
your_table 
WHERE 
your_timestamp_column BETWEEN 'start_timestamp' AND 'end_timestamp'
这将是一个使用BETWEEN语句的示例,它将帮助服务器完成您正在尝试的任务

根据Ollie的评论,BETWEEN是包含的,所以我的语法是基于结果中故意包含的结束时间。如其他人所述,如果您希望包括开头但不包括结尾,则必须使用
=
='start\u timestamp'和您的\u timestamp\u列<'end\u timestamp'

警告-在没有Visual Studio和正确错误处理的情况下,从我的头顶开始编写代码,但这应该向您展示如何使用参数化查询正确设置必要的参数值。这可能包含语法错误,但同样,它应该向您显示足够的信息,让您继续。我添加了注释来解释相关代码

private System.Data.DataTable ExecuteSql(DateTime BusinessDate)
{

    System.Data.DataTable ReturnValue = new System.Data.DataTable;
    string sql = "Select * From myTable WHERE TimestampColumn >= @StartDate AND TimestampColumn < @EndDate";
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDb.Command(connectionString, sql);

    // For start date, we can't assume the user has passed in a date with a 
    // midnight time, so first, use DateTime.Date to get JUST the date at midnight, 
    // then add 7 hours to get to the desired start time.
    // For example, if the calling code had passed in 1/1/2001 8:00 AM we would use
    // the .Date property to get it to 1/1/2001 12:00 AM
    // and then add 7 hours.

    cmd.Parameters.Add(@StartDate, BusinessDate.Date.AddHours(7));

    // The end date - same logic, but instead of adding 7 hours, add 31 
    // (24 hours + 7 hours = 31 hours)

    cmd.Parameters.Add(@EndDate, BusinessDate.Date.AddHours(31));  // 24 + 7
    System.Data.OleDb.OleDbDataAdapter ad = new System.Data.OleDb.OleDbDataAdapter(cmd);
    ad.Fill(ReturnValue)    

    return ReturnValue;
}
private System.Data.DataTable ExecuteSql(DateTime-BusinessDate)
{
System.Data.DataTable ReturnValue=新的System.Data.DataTable;
string sql=“从myTable中选择*,其中TimestampColumn>=@StartDate和TimestampColumn<@EndDate”;
System.Data.OleDb.OleDbCommand cmd=new System.Data.OleDb.OleDb.Command(connectionString,sql);
//对于“开始日期”,我们不能假设用户已输入带有
//午夜时间,所以首先,使用DateTime.Date来获取午夜的日期,
//然后再加上7小时以达到所需的开始时间。
//例如,如果调用代码在2001年1月1日上午8:00通过,我们将使用
//.Date属性将其设置为2001年1月1日凌晨12:00
//然后再加上7小时。
cmd.Parameters.Add(@StartDate,BusinessDate.Date.AddHours(7));
//结束日期-相同的逻辑,但不是增加7小时,而是增加31小时
//(24小时+7小时=31小时)
cmd.Parameters.Add(@EndDate,BusinessDate.Date.AddHours(31));//24+7
System.Data.OleDb.OleDbDataAdapter ad=新的System.Data.OleDb.OleDbDataAdapter(cmd);
ad.Fill(返回值)
返回值;
}
从myTable中选择*,其中时间戳>='start'和时间戳<'end'
请注意,结束被排除在外,因为否则您将在接下来的查询中作为第二天的开始再次查询它


与之间的
运算符包括左运算符和右运算符。如果时间戳恰好位于
0700
,则必须确定它是属于上一个期间还是新期间。

由于包含日期范围,所以中间值不正确。由于包含日期范围,所以中间值不正确。由于包含日期范围,所以中间值不正确。
private System.Data.DataTable ExecuteSql(DateTime BusinessDate)
{

    System.Data.DataTable ReturnValue = new System.Data.DataTable;
    string sql = "Select * From myTable WHERE TimestampColumn >= @StartDate AND TimestampColumn < @EndDate";
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDb.Command(connectionString, sql);

    // For start date, we can't assume the user has passed in a date with a 
    // midnight time, so first, use DateTime.Date to get JUST the date at midnight, 
    // then add 7 hours to get to the desired start time.
    // For example, if the calling code had passed in 1/1/2001 8:00 AM we would use
    // the .Date property to get it to 1/1/2001 12:00 AM
    // and then add 7 hours.

    cmd.Parameters.Add(@StartDate, BusinessDate.Date.AddHours(7));

    // The end date - same logic, but instead of adding 7 hours, add 31 
    // (24 hours + 7 hours = 31 hours)

    cmd.Parameters.Add(@EndDate, BusinessDate.Date.AddHours(31));  // 24 + 7
    System.Data.OleDb.OleDbDataAdapter ad = new System.Data.OleDb.OleDbDataAdapter(cmd);
    ad.Fill(ReturnValue)    

    return ReturnValue;
}
SELECT * FROM myTable WHERE timestamp >= 'start' AND timestamp < 'end'