gpt4 book ai didi

c# - 在 C# 中读取时来自 SQL 的 DateTime 减少了 1 秒

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:03 25 4
gpt4 key购买 nike

这是我的C#代码的一部分

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);
}

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


2014-01-28T06:49:29
但是当我从 C# 读取它时,我得到
2014-01-28T06:49:28
这里是SQL存储过程参数


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

最佳答案

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

尝试在存储过程和基础 SQL 表中使用 DATETIME2

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

关于c# - 在 C# 中读取时来自 SQL 的 DateTime 减少了 1 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com