我想用 C# 创建一个登录表单。如果用户名和密码正确,则不会出现错误,但如果错误,则在此行中出现错误:
int count = Convert.ToInt32(cmd.ExecuteScalar().ToString());
登录表单代码:
if (textBox1.Text != "" & textBox2.Text != "")
{
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT id FROM users WHERE Login = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "'", conn);
int count = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (count > 0)
{
SqlCeDataReader reader = cmd.ExecuteReader();
reader.Read();
int logged_id = Convert.ToInt16(reader["id"]);
SqlCeCommand cmd1 = new SqlCeCommand("SELECT Login, Sex, Weight, Height, Age, PhyActi FROM vartotojai WHERE ID = '"+ logged_id +"'",conn);
SqlCeDataReader reader1 = cmd1.ExecuteReader();
reader1.Read();
textBox9.Text = Convert.ToString(reader1["Sex"]);
textBox10.Text = Convert.ToString(reader1["Weight"]);
textBox12.Text = Convert.ToString(reader1["Height"]);
textBox11.Text = Convert.ToString(reader1["Age"]);
textBox13.Text = Convert.ToString(reader1["Phyacti"]);
panel1.Visible = false;
dataGridView1.Visible = false;
MessageBox.Show("Loggen In!");
toolStripStatusLabel1.Text = "Welcome, " + Convert.ToString(reader1["Login"]);
}
else
MessageBox.Show("User Not Found!");
conn.Close();
}