我正在尝试在带有日期列的 PostgreSQL 表中插入一行。在 UI 上,我有一个 DateTimePicker,用户可以在其中选择正确的日期。到目前为止,我得到了这个:
在用户界面上:
objPresupuesto.date = this.dtpFechaPres.Value.ToString("yyyy-MM-dd");
关于插入行的方法:
NpgsqlCommand query = new NpgsqlCommand("insert into presupuesto(presupuesto, codigo, descripcion, fecha, cliente, proyecto, total) values(nextval('presupuesto_presupuesto_seq'), @codigo, @descripcion, @fecha, @cliente, @proyecto, @total);Select lastval();", conn);
...
query.Parameters.Add(new NpgsqlParameter("fecha", NpgsqlDbType.Date, 0, "fecha"));
...
query.Parameters[2].Value = obj.date.toString;//without the toString it also fails
它抛出这个异常:
Specified cast is not valid.
obj.date 值为 2011-04-29。尝试在周围加上单引号,但它也失败了。
数据库列类型是日期。
以前有人做过吗?有任何想法吗?
我检查了这个链接搜索并回答,但它没有帮助。谢谢