我正在尝试将 PostgreSQL 数据库连接到 C# 控制台应用程序。这是我的代码:
static void Main(string[] args)
{
string connstring = String.Format("Server=localhost;Port=5432;" +
"User Id=postgres;Password=admin;Database=mydb;");
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
string sql = string.Format("CREATE TABLE public.stvari2 ime text, broj smallint, id integer)");
NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
我确实从 Postgres 复制了反向工程的 SQL 查询,所以我怀疑这就是问题所在。无论如何,当我运行它时,我得到一个异常:
An unhandled exception of type 'Npgsql.PostgresException' occurred in Npgsql.dll
在最后一行(executeNonQuery)。 有什么想法吗?
最佳答案
查看您的 SQL 字符串 - 您是否缺少 ( ?
string sql = string.Format("CREATE TABLE public.stvari2 ime text, broj smallint, id integer)");
在这里你不需要另一个( --> public.stvari2( ime
string sql = string.Format("CREATE TABLE public.stvari2( ime text, broj smallint, id integer)");
关于c# - C# 中的 Postgres 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284329/