[英]PostgreSQL - ERROR: column “id” is of type uuid but expression is of type integer
I'm attempting to write some data to a PostgreSQL database using C# .NET via the ODBC driver Driver={PostgreSQL ANSI(x64)}
.我正在尝试通过 ODBC 驱动Driver={PostgreSQL ANSI(x64)}
使用 C# .NET 将一些数据写入 PostgreSQL 数据库。 Does this driver have any known data type matching issues?此驱动程序是否有任何已知的数据类型匹配问题?
I'm trying to send in a GUID and a DateTime field.我正在尝试发送 GUID 和 DateTime 字段。 The database columns are typed as a uuid
and a timestamp without timezone
.数据库列被输入为一个uuid
和一个timestamp without timezone
的timestamp without timezone
。 When I attempt to send either one, I get back errors.当我尝试发送任何一个时,我都会收到错误消息。 When I send the GUID into the uuid field, I get back this error: ERROR: column "id" is of type uuid but expression is of type integer
当我将 GUID 发送到 uuid 字段时,我收到此错误: ERROR: column "id" is of type uuid but expression is of type integer
For the datetime, I get the following: ERROR: column "createdate" is of type timestamp without time zone but expression is of type integer;
对于日期时间,我得到以下信息: ERROR: column "createdate" is of type timestamp without time zone but expression is of type integer;
Here is some short code that illustrates the problem.这是一些说明问题的简短代码。 This fails:这失败了:
var p = new OdbcParameter
{
ParameterName = "@1",
DbType = DbType.Guid, // I also tried OdbcType = OdbcType.UniqueIdentifier
Value = Guid.NewGuid()
};
var sql = "INSERT INTO mytable (id) VALUES (@1)";
using(var conn = new OdbcConnection(_connectionString))
{
conn.Open();
using (var command = new OdbcCommand(sql))
{
command.CommandType = CommandType.Text;
command.Parameters.Add(p);
command.Connection = conn;
command.ExecuteNonQuery();
}
}
As a sanity check, I tried it with SQL-injection and it works:作为完整性检查,我使用 SQL 注入进行了尝试,并且可以正常工作:
var sql = "INSERT INTO mytable (id) VALUES ('" + Guid.NewGuid().ToString("N") + "')";
using (var conn = new OdbcConnection(_connectionString))
{
conn.Open();
using (var command = new OdbcCommand(sql))
{
command.CommandType = CommandType.Text;
command.Connection = conn;
command.ExecuteNonQuery();
}
}
If I involve the date time field, I get the same results, different error message.如果我涉及日期时间字段,我会得到相同的结果,不同的错误消息。
Is the ODBC drive mismatching something? ODBC 驱动器是否不匹配?
UPDATE更新
Thanks to comments below, this appears to work.感谢下面的评论,这似乎有效。 I named the parameter "x" and used ?我将参数命名为“x”并使用 ? (question-mark) in the INSERT statement. (问号)在 INSERT 语句中。 It appears that the parameters are positional.看来参数是位置性的。
var p = new OdbcParameter
{
ParameterName = "x",
DbType = DbType.Guid,
Value = Guid.NewGuid()
};
var sql = "INSERT INTO mytable (id) VALUES (?)";
using(var conn = new OdbcConnection(_connectionString))
{
conn.Open();
using (var command = new OdbcCommand(sql))
{
command.CommandType = CommandType.Text;
command.Parameters.Add(p);
command.Connection = conn;
command.ExecuteNonQuery();
}
}
I don't think @1
is a correct name of DB parameter.我不认为@1
是 DB 参数的正确名称。 Change it and try.改变它并尝试。 I tried out in sqlfiddle with PostgreSQL9.3...it does not recognize @1
as a parameter, considers it as a integer.我在sqlfiddle 中使用 PostgreSQL9.3 进行了尝试……它无法将@1
识别为参数,而是将其视为整数。
I had this error there:我在那里遇到了这个错误:
ERROR: column "data" is of type uuid but expression is of type integer Hint: You will need to rewrite or cast the expression.错误:“数据”列的类型为 uuid,但表达式的类型为整数 提示:您需要重写或转换表达式。 Position: 36职位:36
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.