c# - C# 中的 Postgres 异常

标签 c# visual-studio postgresql

我正在尝试将 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/

相关文章:

c# - Visual Studio 缩进设置为两个空格而不是四个空格制表符

visual-studio - 如何从 Visual Studio 上的项目中删除作为链接添加的文件?

sql - 反斜杠在 LIKE 子句中工作不正确

c# - 在数据库中插入多条记录的最快方法

c# - 没有事件窗口的屏幕截图

c# - "async Task then await Task"与 "Task then return task"

c# - 什么是cvtres.exe?

c# - 调试程序集加载问题

postgresql - Postgres where 子句比较时间戳

postgresql - 使 Sequelize 使用返回 : true for all mutations