简体   繁体   English

C#连接到Postgres数据库

[英]C# connecting to a postgres database

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? NpgsqlConnection字符串是否正确编写? Could the database be protected from remote access? 可以保护数据库免受远程访问吗?

How could I fix this problem? 我该如何解决这个问题?

Thanks in advance! 提前致谢!

You never assign your NpgsqlConnection to your NpgsqlCommand and 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 NpgsqlConnection in a using() -statement is a good idea to make sure that the connection is always closed, even if there is an exception. 另外,至少将NpgsqlConnection包装在using() statement中是一个好主意,以确保即使有异常,连接也总是关闭的。

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();
            }
        }
    }
}

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. 同样-为了方便用户使用和易于捕获错误,可能值得在try catch语句中包装conn.open()。

Edit 1: 编辑1:

Just did a little reading. 只是做了一点阅读。 Does NpgsqlCommand need parameters to be passed to it? NpgsqlCommand是否需要将参数传递给它? just in pseudo code, something like; 只是用伪代码,就像;

NpgsqlCommand cmd = new NpgsqlCommand(query, conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM