根据书上的案例自己编写了一个餐厅管理系统可是在运行时登陆时输入账户密码 却提示输入用户名密码错误代码如下
private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnSubmit_Click(sender, e);
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (txtPwd.Text == "")
{
MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//SqlConnection conn = BaseClass.DBConn.CyCon();
string str = "Data Source=127.0.0.1;Initial Catalog=cantingguanlixitong;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "' and themima='" + txtPwd.Text + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "'", conn);
sdr = cmd.ExecuteReader();
sdr.Read();
string UserPower = sdr["quanxian"].ToString().Trim();
conn.Close();
Form2 main = new Form2 ();
main.Show();
private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnSubmit_Click(sender, e);
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (txtPwd.Text == "")
{
MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
//SqlConnection conn = BaseClass.DBConn.CyCon();
string str = "Data Source=127.0.0.1;Initial Catalog=cantingguanlixitong;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "' and themima='" + txtPwd.Text + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "'", conn);
sdr = cmd.ExecuteReader();
sdr.Read();
string UserPower = sdr["quanxian"].ToString().Trim();
conn.Close();
Form2 main = new Form2 ();
main.Show();
if (sdr.HasRows)
{
//此处为何又查询一次数据库?
cmd = new SqlCommand("select * from quanxian where thename='" + txtName.Text + "'", conn);
sdr = cmd.ExecuteReader();
sdr.Read();
string UserPower = sdr["quanxian"].ToString().Trim();
conn.Close();
Form2 main = new Form2 ();
main.Show();
string str = "Data Source=127.0.0.1;Initial Catalog=cantingguanlixitong;Integrated Security=True";
========
string str = "Data Source=.\sqlexpress;Initial Catalog=cantingguanlixitong;Integrated Security=True";你的数据库是否是express版本