尝试以 C# 形式运行登录时出现"SqlException was unhandled"错误


{
    SqlConnection conn = new SqlConnection(global::Database_test.Properties.Settings.Default.Database1ConnectionString);
    SqlCommand cmd = new SqlCommand("Select * from Login where username='" + textBox5.Text + "' and Password = '" + textBox6.Text + "'conn");
    cmd.Connection = conn;
    conn.Open();
    SqlDataReader re = cmd.ExecuteReader();
    if (re.Read())
    {
        MessageBox.Show("Login Sucessful");
    }
    else
    {
        MessageBox.Show("Login Failed");
    }
}

更改此项:

SqlCommand cmd = new SqlCommand("Select * from Login where username='" + textBox5.Text + "' and Password = '" + textBox6.Text + "'conn");

到此:

SqlCommand cmd = new SqlCommand("Select * from Login where username='" + textBox5.Text + "' and Password = '" + textBox6.Text + "'", conn);

在提出任何建议之前,您可以向我们发送您的InnerException描述,或者您可能需要提供更多信息,因为此异常是一般性的。然而,以下是一些提示,我建议你看看:

  1. 注意以下行末尾的"conn":

    SqlCommand cmd = new SqlCommand("Select * from Login where username='" + textBox5.Text + "' and Password = '" + textBox6.Text + "'conn");
    

"conn"应作为SqlCommand的第二个参数传递:

 SqlCommand cmd = new SqlCommand("Select * from Login where username='" + textBox5.Text + "' and Password = '" + textBox6.Text + "'",conn);

2.注意使用onn。Open();表示我总是把它放在下面的块中:

if(conn.State != ConnectionState.Open)
   conn.Open();

在此处查找有关ConnectionState枚举的更多信息

相关内容

最新更新