本文介紹了PostgreSQL 日期 C# DateTime的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我正在使用此查詢(xún)向表中插入日期:
I'm using this query to insert into a table a date:
DateTime dt = DateTime.Now;
String query = "Insert into "Table1" ("tableDate") values (:date1)";
NpgsqlConnection conn = new NpgsqlConnection(conex.getConnection());
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
NpgsqlParameter param = new NpgsqlParameter(":date1",NpgsqlTypes.NpgsqlDbType.Date);
param.Value = dt.ToShortDateString();
cmd.Parameters.Add(param); // <----------Here i get the error
cmd.ExecuteNonQuery();
conn.Close();
這就是我在表中插入任何其他值的方式,它可以工作!所以肯定是日期格式什么的有錯(cuò)誤,但是我找不到答案.
And this is how I am inserting in the table any other value and it works! So there must be an error in the date format or something, but I can't find the answer.
推薦答案
別做
param.Value = dt.ToShortDateString();
離開(kāi)
param.Value = dt;
你正試圖在 DateTime 字段中插入一個(gè)字符串 - 肯定 PostgreSQL 會(huì)抱怨.
You're trying to insert a string into DateTime field - sure PostgreSQL will complain.
如果你想避免丟失時(shí)間信息,請(qǐng)使用 NpgsqlTypes.NpgsqlDbType.Timestamp
use NpgsqlTypes.NpgsqlDbType.Timestamp if you want to avoid losing the time information
這篇關(guān)于PostgreSQL 日期 C# DateTime的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!