How to insert DateTime (or DateTime2) into Dapper?

I am trying to do the following:

var p = new DynamicParameters();
p.Add(...);
p.Add(...);
// ideally I want to insert null for end date, but I also tried DateTime.MinValue
// or some other date, but doesn't help
p.Add("@endDate", null, DbType.DateTime, ParameterDirection.Input);
using (var con = new SqlConnection(ConnectionString))
{
    // stored procedure does 'select SCOPE_IDENTITY()' to return the id of inserted row.
    int id = con.Query<int>("StoredProcedureName", p, commandType: CommandType.StoredProcedure).First();
}

      

This is throwing an "InvalidCastException" and I believe it is date related. I previously had datetime2 (20) for endDate in sql db, changed it to datetime to see if it fixes it, but doesn't. Can anyone please help?

+3


source to share


1 answer


The invalid cast here is what it SCOPE_IDENTITY()

actually returns decimal

. The parameters are working fine.



+2


source







All Articles