2016-03-02 68 views
0

写作查询开始,这里就是我走到这一步:为一个PostgreSQL数据库

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。在它说:

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说,我需要分裂查询。这是我做的:

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

它的工作原理。

+0

你是否正在使用Mono实例运行此操作? – ohiodoug

+0

不,我不是@ohiodoug – John

回答

2

,而不是集模式的尝试:

set search_path to 'schema'

这是什么set schema应该别名,但也许该别名不工作Npgsql的

其他的事情,我会尝试是运行设置search_path作为它自己的命令,然后将查询作为下一个命令运行。

第三件事是检查,如果用户有权读取该架构中的表。

相关问题