写作查询开始,这里就是我走到这一步:为一个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();
它的工作原理。
你是否正在使用Mono实例运行此操作? – ohiodoug
不,我不是@ohiodoug – John