首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写PostgreSQL数据库的查询

编写PostgreSQL数据库的查询
EN

Stack Overflow用户
提问于 2016-03-02 19:12:33
回答 1查看 816关注 0票数 1

首先,我到目前为止得到的是:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;         // Npgsql .NET Data Provider for PostgreSQL

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            NpgsqlConnection conn = new NpgsqlConnection ("Server=127.0.0.1;Port=5432;UserId=postgres;Password=test;Database=dbab;") ;
            conn.Open() ;

            // Define a query
            NpgsqlCommand cmd = new NpgsqlCommand("set schema 'data'; select test_run.id from test_run;", conn);
            cmd.ExecuteNonQuery();

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(0));
                }
            }

            // Close connection
            conn.Close();

            System.Console.WriteLine("Press any key to close...");
            Console.ReadKey(); 
        }
    }
}

每当我运行这个程序时,我都会得到NpgsqlException was unhandled。根据这一条,它说:

代码语言:javascript
复制
An unhandled exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll
Additional information: External component has thrown an exception.

当我单击查看详细信息时,如下所示:{"42P01: relation \"test_run\" does not exist"}

当我看到这个时,我想可能我的查询搞砸了,但实际上很好,因为我在pgAdmin中运行它,在查找'test_run‘表时没有问题。

我在Nuget中安装了Npgsql -Version 3.0.5和Mono.Security 3.2.3.0。我还将两个..dll(不确定如何签入此)添加到GAC中。

此外,我卸载了Npgsql,再次安装了它,但是得到了2.2.3版本,仍然遇到了同样的问题。我现在回来了3.0.5版。

我不知道怎么解决这个问题,请帮帮忙。

编辑:

正如jackmott所说,我需要分拆查询。我所做的是:

代码语言:javascript
复制
NpgsqlCommand cmd = new NpgsqlCommand("set schema 'data';", conn);
cmd.ExecuteNonQuery();
NpgsqlCommand cmd2 = new NpgsqlCommand("select test_run.id from test_run;", conn);
cmd2.ExecuteNonQuery();

而且它是有效的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-02 19:38:30

不要设置模式,而是尝试:

set search_path to 'schema'

这是set schema应该别名的名称,但是这个别名在NpgSQL中可能不起作用

我要尝试的另一件事是将set search_path作为自己的命令运行,然后将查询作为下一个命令运行。

第三件要检查的事情是,用户是否有权在该模式中读取该表。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35756097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档