【问题标题】:Npgsql has forgotten all data typesnpgsql忘记了所有数据类型
【发布时间】:2018-09-20 07:46:10
【问题描述】:

我正在使用 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.

如果我按照常见问题解答 (AllResultTypesAreUnknown=true) 中的建议,我可以获得字符串,但为什么不能获得 int?

昨天,这对我来说工作正常,但我无法弄清楚发生了什么变化或损坏,因此我无法设置或获取任何数据类型。我已经删除并重新创建了服务器,没有运气。重新安装了 C# 驱动程序,也没有运气。这是一个精简的代码示例,上面发生在我的“真实”代码中,带有参数化值以及检索到的结果。

【问题讨论】:

    标签: c# npgsql cockroachdb


    【解决方案1】:

    为了保护服务器,我创建了一个新用户,该用户对查询的表具有 SELECT 和 INSERT 权限,但对数据库没有权限。在重新阅读 CockroachDb C# 入门后,我意识到示例的用户拥有完整的数据库权限,并且凭直觉尝试了相同的操作。我猜驱动程序需要访问某个地方的模式表才能确定查询的数据类型(我们将忽略我提供的示例不应该需要它的事实)。

    GRANT SELECT ON DATABASE database TO user;
    

    【讨论】:

    • 架构最有可能是information_schema 和视图columns
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2018-05-09
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多