ASP.NET postgresql NpgsqlDbType.Bit 不支持 BitArray?
ASP.NET postgresql NpgsqlDbType.Bit doesn't support BitArray?
运行 变成了一个似乎与文档相矛盾的类型错误...
在我的本地计算机上创建了一个包含 2 列的 postgresql 表格(SystemID [as uuid],TrackingIDs [as Bit[] with size 256])。
在 C# 中 ASP.NET 我收到错误:
42804: column "TrackingIDs" is of type bit[] but expression is of type
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 核心 2.2
编辑:丢失 "NpgsqlDbType.Bit | NpgsqlDbType.Array"。但是我现在只得到:
22026: bit string length 1 does not match type bit(256)
因此 PostgreSQL 中的位可以是任意大小或可以被认为是复数。例如,从一个字节的角度考虑。所以解决方案是只创建一个长度为 256 的 "bit" 类型的列。然后我可以为其设置一个 BitArray(256)。
运行 变成了一个似乎与文档相矛盾的类型错误...
在我的本地计算机上创建了一个包含 2 列的 postgresql 表格(SystemID [as uuid],TrackingIDs [as Bit[] with size 256])。
在 C# 中 ASP.NET 我收到错误:
42804: column "TrackingIDs" is of type bit[] but expression is of type 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 核心 2.2
编辑:丢失 "NpgsqlDbType.Bit | NpgsqlDbType.Array"。但是我现在只得到:
22026: bit string length 1 does not match type bit(256)
因此 PostgreSQL 中的位可以是任意大小或可以被认为是复数。例如,从一个字节的角度考虑。所以解决方案是只创建一个长度为 256 的 "bit" 类型的列。然后我可以为其设置一个 BitArray(256)。