本文介紹了PostgreSQL 日期 C# DateTime的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在使用此查詢向表中插入日期:
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();
這就是我在表中插入任何其他值的方式,它可以工作!所以肯定是日期格式什么的有錯誤,但是我找不到答案.
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();
離開
param.Value = dt;
你正試圖在 DateTime 字段中插入一個字符串 - 肯定 PostgreSQL 會抱怨.
You're trying to insert a string into DateTime field - sure PostgreSQL will complain.
如果你想避免丟失時間信息,請使用 NpgsqlTypes.NpgsqlDbType.Timestamp
use NpgsqlTypes.NpgsqlDbType.Timestamp if you want to avoid losing the time information
這篇關于PostgreSQL 日期 C# DateTime的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!