【发布时间】:2019-05-20 12:22:47
【问题描述】:
遇到了一个似乎与文档相矛盾的类型错误...
在我的本地计算机上创建了一个包含 2 列的 postgresql 表(SystemID [as uuid],TrackingIDs [as Bit[] with size 256])。
在 C# ASP.NET 中出现错误:
42804:“TrackingIDs”列的类型为 bit[],但表达式的类型为 位
我看不到“NpgsqlDbType.BitArray”的选项,但文档说“NpgsqlDbType.Bit”应该接受 C# BitArray 对象类型:https://www.npgsql.org/doc/types/basic.html
这是我的 C# 代码示例:
using (var connection = new NpgsqlConnection(DBUtils.connectionString))
{
try
{
connection.Open();
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO hosts VALUES(@SystemID, @TrackingIDs)";
cmd.Parameters.AddWithValue("@SystemID", NpgsqlDbType.Uuid, systemID);
cmd.Parameters.AddWithValue("@TrackingIDs", NpgsqlDbType.Bit, new BitArray(256));
return cmd.ExecuteNonQuery() != 0 ? "Success" : "Failed";
}
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
connection.Close();
}
}
pgAdmin4:3.6
Npgsql 版本:4.0.4
PostgreSQL 版本:11.1
操作系统:Win10 x64
ASP.NET:.NET Core 2.2
编辑:缺少“NpgsqlDbType.Bit | NpgsqlDbType.Array”。但是我现在得到:
22026: 位串长度 1 与类型 bit(256) 不匹配
【问题讨论】:
标签: asp.net postgresql parameters bit bitarray