代码之家  ›  专栏  ›  技术社区  ›  Marc Bernier

Npgsql已忘记所有数据类型

  •  2
  • Marc Bernier  · 技术社区  · 8 年前

    我正在使用Npgsql连接到不安全的本地CockroachDb服务器,如下所示:

    using (NpgsqlConnection connection = new NpgsqlConnection(@"Port=26257;Username=user;Database=thedata;Host=localhost")) {
      connection.Open();
      NpgsqlCommand command = new NpgsqlCommand("SELECT 1 AS records", connection);
      command.CommandType = CommandType.Text;
    
      using (NpgsqlDataReader reader = command.ExecuteReader()) {
        while (reader.Read()) {
          Console.WriteLine(reader["records"].GetType());
        }
      }
    }
    

    GetType()失败,出现异常:

    The field 'records' has a type currently unknown to Npgsql (OID 20). You can retrieve it as a string by marking it as unknown, please see the FAQ.
    

    如果我遵循FAQ中的建议(AllResultTypesAreUnknown=true),我可以得到一个字符串,但为什么不能得到一个int?

    昨天,这对我来说很好,但我无法找出发生了什么变化或损坏了什么,因此无法设置或获取任何数据类型。我已经删除并重新创建了服务器,运气不好。重新安装了C#驱动程序,也不走运。这是一个简化的代码示例,上面发生在我的带有参数化值和检索结果的“real”代码中。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Marc Bernier    8 年前

    为了保护服务器的安全,我创建了一个新用户,该用户对查询表具有选择和插入权限,但对数据库没有权限。在重新浏览了CockroachDb C#primer之后,我意识到该示例的用户拥有完全的数据库权限,并凭直觉尝试了同样的方法。我猜驱动程序需要访问某个模式表,以便找出查询的数据类型(我们将忽略我提供的示例不需要这样的事实)。

    GRANT SELECT ON DATABASE database TO user;