2013-02-18 88 views
3

我試圖做到以下幾點:如何在Dapper中插入DateTime(或DateTime2)?

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

這扔「InvalidCastException的」,我相信這是becase的日期。我以前有datetime2(20)在sql數據庫中的endDate,將其更改爲日期時間,以查看它是否修復它,但沒有。誰能幫忙?

回答

2

這裏輸入的無效是SCOPE_IDENTITY()實際上返回decimal。參數工作正常。

+0

感謝您的快速回復。我確實搜索了與Dapper相關的問題,並且發現了一些關於SCOPE_IDENTITY()的討論,但是我沒有足夠的重視並意識到它與這個問題有關。 – Shankar 2013-02-18 03:47:54

相關問題