我有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();
}
答案 0 :(得分:2)
如果表名登录正确,请检查您的数据库。顺便说一下,您需要过滤输入值,因为您的代码容易受到SQL注入攻击。
您还应该尝试更改表或数据库名称,它们都被称为登录,这可能会导致问题。
答案 1 :(得分:1)
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
登录是表名
如果这是正确的,那么检查表登录是否存在于数据库登录中。
ERROR: 42P01: relation "login" does not exist
关系表示表
此外,您不能在Postgres中使用以大写字母开头的表名。如果您的表名以大写字母开头,则需要将其括在双引号内。