簡體   English   中英

從asp.net(C#)System.dateTime插入/更新SQL 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() + "' ");

使用查看sqlsql的存儲方式

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM