[英]C# asp.net core webapi throwing "The JSON value could not be converted to System.Nullable`1[System.DateTime]
[英]Insert/Update SQL DateTime From asp.net(C#) System.dateTime
這是我想要做的:
注意:我不想使用參數化查詢。
碼:
refresh_query.Append("UPDATE Table SET ");
System.DateTime now = System.DateTime.Now;
refresh_query.Append("date='" +now + "' ");
refresh_query.Append("WHERE user_id='1'");
這導致此查詢:
UPDATE Table SET date='25/02/2014 12:04:00'
WHERE user_id='1'
收到以下錯誤:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
but was not handled in user code
Additional information: The conversion of a varchar data type to a datetime data
type resulted in an out-of-range value.
幾乎每個數據庫引擎都有返回當前日期和時間的東西。 為什么不使用它而不是傳遞字符串?
更改
refresh_query.Append("date='" +now + "' ");
至
refresh_query.Append("date='" +now.ToString() + "' ");
使用查看sql
在sql
的存儲方式
select getdate()
在你的代碼中
refresh_query.Append("date='" +now.ToString(_formatExpression_) + "' ");
其中formatExpression - 對應於SQL
中日期格式的格式( http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(v=vs.110).aspx#properties )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.