【问题标题】:ASP.NET postgresql NpgsqlDbType.Bit doesn't support BitArray?ASP.NET postgresql NpgsqlDbType.Bit 不支持 BitArray?
【发布时间】: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


    【解决方案1】:

    所以 PostgreSQL 中的 bit 可以是任意大小,也可以被认为是复数。例如,从一个字节的角度考虑。所以解决方案是只创建一个长度为 256 的“位”类型列。然后我可以为其设置一个 BitArray(256)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2015-04-29
      • 2016-04-26
      相关资源
      最近更新 更多