繁体   English   中英

如何连接C#和PostgreSQL

[英]How to connect C# and PostgreSQL

我有2个表单,first1有用户名,密码框和登录按钮。 当我点击登录按钮时,它将比较PostgresSQL中的用户名密码。 但是这条线路出错了

NpgsqlDataReader dr = cmd.ExecuteReader(); [错误:42P01:关系“登录”不存在]

这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        bool blnfound = false;
        NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=admin123;Database=Login");
        conn.Open();
        NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM login WHERE name='" + tb1.Text + "' and password = '" + tb2.Text + "'",conn);
        NpgsqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            blnfound = true;
            Form2 f5 = new Form2();
            f5.Show();
            this.Hide();
        }

        if (blnfound == false)
        {
            MessageBox.Show("Name or password is incorrect", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            dr.Close();
            conn.Close();
        }

如果表名登录正确,请检查您的数据库。 顺便说一句,您需要过滤输入值,因为您的代码容易受到SQL注入攻击。

您还应该尝试更改表或数据库名称,它们都被称为登录,这可能会导致问题。

Server=127.0.0.1;Port=5432;User Id=postgres;Password=admin123;**Database=Login**

登录是数据库名称

"SELECT * FROM **login** WHERE name='" + tb1.Text + "' and password = '" + tb2.Text + "'",conn

登录是表名

如果这是正确的,那么检查表登录是否存在于数据库LOGIN中

ERROR: 42P01: relation "login" does not exist

关系意味着

此外,您不能在Postgres中使用以大写字母开头的表名。 如果您的表名以大写字母开头,则需要将其括在双引号内。

暂无
暂无

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

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