C# Npgsql查询中的数据类型问题
我试图在PostgreSQL表中插入一行,其中包含日期列。在UI上,我得到了一个DateTimePicker,用户可以在其中选择正确的日期。到目前为止,我得到了这个: 在UI上:C# Npgsql查询中的数据类型问题,c#,postgresql,npgsql,C#,Postgresql,Npgsql,我试图在PostgreSQL表中插入一行,其中包含日期列。在UI上,我得到了一个DateTimePicker,用户可以在其中选择正确的日期。到目前为止,我得到了这个: 在UI上: objPresupuesto.date = this.dtpFechaPres.Value.ToString("yyyy-MM-dd"); 在插入行的方法上: NpgsqlCommand query = new NpgsqlCommand("insert into presupuesto(presupuesto, c
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。尝试使用单引号,但也失败了
数据库列类型为date
以前有人这样做过吗?有什么想法吗
我检查了这个搜索和aswer,但没有帮助。
谢谢我注意到的第一件事是,在添加参数时,从命令引用带有“@”的值开始,它就缺少“@”
您确定参数[2]是您的“fecha”参数吗?query.Parameters[2]。Value=CDate(obj.date)
npgsqlCommand.Parameters.Add(“fecha”,NpgsqlDbType.Date)。Value=DateTime.Now代码>
获取更可读的代码
对象日期的类型是DateTime结构吗
添加到连接字符串参数Convert Infinity DateTime
有关更多信息,请查看:
嗯,你能给我一个简单的测试用例吗?你试过使用NpgsqlDate对象吗?@FranciscoJunior我试过使用NpgsqlDate,它可以工作,但有点棘手,我不知道是否有直接的方法可以做到这一点。非常感谢。是的,我也认为可以更直接一些。你能给我提供一个简单的基于控制台的测试应用程序让我看看吗?另外,如果可能的话,请在:bugs.npgsql.org上填写一份关于这个问题的bug报告,谢谢