我尝试将c#中的DateTime传递给SQL Server存储过程,但是,其中一个日期(即toDate)被添加了1秒。对于fromDate,时间为 2017年7月1日上午12:00:00,对于toDate,时间为2017年7月31日晚上11:59:59。但是,当涉及到SQL Server Profiler时,我发现toDate更改为2017-08-01 00:00:00。
以下是我的C#代码,在该代码上2017年7月31日晚上11:59:59传递给toDate
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SelectUsage4", conn))
{
SqlParameter param = null;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 600;
param = cmd.Parameters.AddWithValue(ConvertToParam("ids"), ids ?? (object)DBNull.Value);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
param = cmd.Parameters.AddWithValue(ConvertToParam("locationIDs"), locationIDs ?? (object)DBNull.Value);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
param = cmd.Parameters.AddWithValue(ConvertToParam("indicatorIDs"), indicatorIDs ?? (object)DBNull.Value);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
param = cmd.Parameters.AddWithValue(ConvertToParam("fromDate"), fromDate ?? (object)DBNull.Value);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.DateTime;
param = cmd.Parameters.AddWithValue(ConvertToParam("toDate"), toDate ?? (object)DBNull.Value);
param.Direction = ParameterDirection.Input;
这是SQL Server Profiler,您可以看到时间已更改为2018-08-01 00:00:00传递给@p_todate
exec SelectUsage4 @p_ids=NULL,@p_locationIDs=N'6,7,8',@p_indicatorIDs=N'94',@p_fromDate='2017-07-01 00:00:00',@p_toDate='2017-08-01 00:00:00',@p_yearly=NULL,@p_monthly=NULL,@p_halfYear1=NULL,@p_halfYear2=NULL,@p_fourMonths1=NULL,@p_fourMonths2=NULL,@p_fourMonths3=NULL,@p_q1=NULL,@p_q2=NULL,@p_q3=NULL,@p_q4=NULL,@p_biMonthly1=NULL,@p_biMonthly2=NULL,@p_biMonthly3=NULL,@p_biMonthly4=NULL,@p_biMonthly5=NULL,@p_biMonthly6=NULL,@p_approvalStatus=NULL,@p_LANGUAGE=N'en-US'
这是存储过程的声明。
ALTER PROCEDURE [dbo].[SelectUsage4](
@p_ids VARCHAR(max)
, @p_locationIDs VARCHAR(max)
, @p_indicatorIDs VARCHAR(max)
, @p_fromDate datetime
, @p_toDate datetime
, @p_yearly smallint
, @p_monthly smallint
, @p_halfYear1 smallint
, @p_halfYear2 smallint
, @p_fourMonths1 smallint
, @p_fourMonths2 smallint
, @p_fourMonths3 smallint
, @p_q1 smallint
, @p_q2 smallint
, @p_q3 smallint
, @p_q4 smallint
, @p_biMonthly1 smallint
, @p_biMonthly2 smallint
, @p_biMonthly3 smallint
, @p_biMonthly4 smallint
, @p_biMonthly5 smallint
, @p_biMonthly6 smallint
, @p_approvalStatus int
, @p_language nvarchar(10)
)
我不太明白为什么要从C#到SQL服务器将1秒添加到toDate。