2011-09-15 71 views
3

轉換日期和/或時間,如果你幫我走出我的錯誤是:從字符串轉換日期和/或時間時轉換失敗時從字符串

轉換失敗。

我的數據庫列是datetime類型的

+0

什麼樣的價值是在'ctime'變量在這一點?這應該給出一個線索,爲什麼它會拋出錯誤。 –

回答

9

使用參數化查詢,你會不會擔心日期格式,或SQL注入,並使用using,以確保您的連接設置。

using (var connection = new SqlConnection(yourConnectionString)) 
using (var command = connection.CreateCommand()) 
{ 
    command.CommandText = "insert into YourTable(Col1, Col2) values(@val1, @val2)"; 
    command.Parameters.AddWithValue("@val1", 123); 
    command.Parameters.AddWithValue("@val2", DateTime.Now); 

    connection.Open(); 

    command.ExecuteNonQuery(); 
} 
2

你也可以GETDATE()SQL的功能,它是SQL的預定義功能

相關問題