dapper执行存储过程时将数据类型datetime转换为smalldatetime时出错

时间:2015-03-10 05:05:21

标签: c# sql-server stored-procedures dapper

我的数据库中有一个表有smalldatetime类型。我有一个存储过程来更新该表中的数据。

存储过程获取@LastLogin smalldatetime参数,其中包含要更新的日期。

在c#中,我有一个使用dapper调用存储过程的代码:

loginDetails.LastLoginDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
db.Execute("stp_UpdateLogin", loginDetails, commandType: CommandType.StoredProcedure);

当我运行时,我收到以下错误:

System.Data.SqlClient.SqlException (0x80131904): Error converting data type datetime to smalldatetime.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3310
   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1310
   at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1185
   at Memoriez.Repositories.UsersRepository.UpdateUser(User user) in f:\Development\Memoriez\Memoriez.Repositories\UsersRepository.cs:line 58
ClientConnectionId:02cae914-507b-4c21-9799-8ec0940fc3d9

如何确保dapper通过SQL期望的内容?

1 个答案:

答案 0 :(得分:1)

那是因为

(DateTime)System.Data.SqlTypes.SqlDateTime.MinValue = 1/1/1753 12:00:00 AM

虽然是

的最小值
SMALLDATETIME  = 1900-01-01 00:00:00

你可以创建一个帮助类:

public static class SqlSmallDateTime
{
    public static readonly SqlDateTime MinValue = new SqlDateTime(new DateTime(1900, 01, 01, 00, 00, 00));
}