postgresql C# 连接到 postgres 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11843547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:09:24  来源:igfitidea点击:

C# connecting to a postgres database

c#databasepostgresql

提问by robertpas

This is my first time connecting to a database, but i'm having some problems

这是我第一次连接到数据库,但我遇到了一些问题

using Npgsql;

namespace DBPrj
 {
class Program
{
    static void Main(string[] args)
    {
        bool boolfound=false;
        NpgsqlConnection conn = new NpgsqlConnection("Server=<ip>; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"); //<ip> is an actual ip address
        conn.Open();

        NpgsqlCommand cmd = new NpgsqlCommand();
        NpgsqlDataReader dr= cmd.ExecuteReader(); //I get InvalidOperationException : The connection is not open.
        if (dr.Read())
        {
            boolfound=true;
            Console.WriteLine("connection established");
        }
        if(boolfound==false)
        {
            Console.WriteLine("Data does not exist");
        }
        dr.Close();
        conn.Close();



    }
}

}

}

What could be the problem? Is the NpgsqlConnection string written correctly? Could the database be protected from remote access?

可能是什么问题呢?NpgsqlConnection 字符串是否正确写入?可以保护数据库免受远程访问吗?

How could I fix this problem?

我该如何解决这个问题?

Thanks in advance!

提前致谢!

回答by Karl-Johan Sj?gren

You never assign your NpgsqlConnectionto your NpgsqlCommandand you don't supply a query to execute for your NpgsqlDataReader, fixing that should solve the immediate problems.

您永远不会将您的分配NpgsqlConnection给您NpgsqlCommand,也不会为您的 提供查询来执行NpgsqlDataReader,修复应该可以解决眼前的问题。

Also, wrapping at least your NpgsqlConnectionin a using()-statement is a good idea to make sure that the connection is always closed, even if there is an exception.

此外,至少NpgsqlConnection在 -using()语句中包装您是一个好主意,以确保连接始终关闭,即使有异常也是如此。

using Npgsql;

namespace DBPrj
{
    class Program
    {
        static void Main(string[] args)
        {
            bool boolfound=false;
            using(NpgsqlConnection conn = new NpgsqlConnection("Server=<ip>; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"))
            {
                conn.Open();

                NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM Table1", conn);
                NpgsqlDataReader dr= cmd.ExecuteReader();
                if (dr.Read())
                {
                    boolfound=true;
                    Console.WriteLine("connection established");
                }
                if(boolfound==false)
                {
                    Console.WriteLine("Data does not exist");
                }
                dr.Close();
            }
        }
    }
}

回答by Shane.C

In your connection string you may be missing a semi-colon at the end of database.

在您的连接字符串中,您可能在数据库末尾缺少分号。

Database=Test1"

may need to be;

可能需要;

Database=Test1;"

also - it may be worth wrapping your conn.open() in a try catch statement for user-friendliness and ease of catching errors.

此外 - 为了用户友好和易于捕捉错误,将 conn.open() 包装在 try catch 语句中可能是值得的。

Edit 1:

编辑1:

Just did a little reading. Does NpgsqlCommand need parameters to be passed to it? just in pseudo code, something like;

刚读了一点。NpgsqlCommand 需要传递参数吗?只是在伪代码中,例如;

NpgsqlCommand cmd = new NpgsqlCommand(query, conn);