dbn*_*urt 1 c# postgresql datetime insert
我正在使用此查询将日期插入表中:
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();
Run Code Online (Sandbox Code Playgroud)
这就是我在表中插入任何其他值的方式,它的工作原理!所以日期格式或其他内容必定存在错误,但我找不到答案.
不要这样做
param.Value = dt.ToShortDateString();
Run Code Online (Sandbox Code Playgroud)
离开吧
param.Value = dt;
Run Code Online (Sandbox Code Playgroud)
你试图在DateTime字段中插入一个字符串 - 确保PostgreSQL会抱怨.
如果要避免丢失时间信息,请使用NpgsqlTypes.NpgsqlDbType.Timestamp
| 归档时间: |
|
| 查看次数: |
8345 次 |
| 最近记录: |