Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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#中读取SQL时的日期时间缩短了1秒_C#_Sql_Sql Server_Datetime - Fatal编程技术网

在C#中读取SQL时的日期时间缩短了1秒

在C#中读取SQL时的日期时间缩短了1秒,c#,sql,sql-server,datetime,C#,Sql,Sql Server,Datetime,这是我的c代码的一部分 在上面的代码中,currentUpdateDTC是我从SQL中读取的日期时间。 它工作正常,但从SQL读取的时间相差1秒。 例如,如果我执行它输出的SQL过程 2014-01-28 06:49:29 但当我从C#读到它时,我明白了 2014-01-28T06:49:28 下面是SQL存储过程参数 @p_UpdateStatus SMALLINT输出, @p_CurrentUpdateDutchTime日期时间输出 将参数定义为DbType.DateTime2,但

这是我的c代码的一部分

在上面的代码中,currentUpdateDTC是我从SQL中读取的日期时间。 它工作正常,但从SQL读取的时间相差1秒。 例如,如果我执行它输出的SQL过程


2014-01-28 06:49:29
但当我从C#读到它时,我明白了

2014-01-28T06:49:28
下面是SQL存储过程参数


@p_UpdateStatus SMALLINT输出,
@p_CurrentUpdateDutchTime日期时间输出

将参数定义为
DbType.DateTime2
,但在存储过程中使用的是精度较低的
DATETIME
。四舍五入可能正在发生

尝试在存储过程和底层SQL表中使用
DATETIME2


另外,我认为您没有告诉我们全部情况,因为SQL Management Studio或默认的
.ToString()
输出都没有使用ISO格式(结果中带有
t
)。也许您可以编辑问题以显示实际输出。

是否存储毫秒?他们可能会把秒数四舍五入。即使是这样,也应该将其传递到c#part rite?中,这取决于精度。有关解释,请参阅本文:SQL的语法是这样的:在遇到名称的任何特定时刻,名称的形式或位置都会立即通知您要命名的对象的类型。因此,实际上没有理由使用前缀、后缀或其他任何东西来区分SQL中的对象类型。(名称与视图是一个明显的例外,但无论如何,您应该尽可能地将视图与表等同对待)
try
{
    Database db = DatabaseFactory.CreateDatabase();

    string sqlCommand = Constants.GET_UPDATE_STATUS; ;
    DbCommand dbCommandWrapper = db.GetStoredProcCommand(sqlCommand);

    db.AddParameter(
        dbCommandWrapper,
        "@p_CurrentUpdatedUTCTime",
        DbType.DateTime2,
        ParameterDirection.InputOutput,
        null,
        DataRowVersion.Current,
        _myDateTime);

    db.AddOutParameter(dbCommandWrapper, "@p_UpdateStatus", DbType.Int16, 10);
    db.ExecuteNonQuery(dbCommandWrapper);
    status = 
      Convert.ToInt16(db.GetParameterValue(dbCommandWrapper, "@p_UpdateStatus"));
    currentUpdatedUTC = 
      db.GetParameterValue(dbCommandWrapper, "@p_CurrentUpdatedUTCTime").ToString();
}
catch (Exception exp)
{
    errorMessage = exp.Message;
    throw new DataAccessException(exp.Message, "UpdateStatusDAL", "GetUpdateStatus", exp);
}