string password = txtPassWord.Text.ToString();
string username = txtUserName.Text.ToString();
string connStr = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=DBApp";
// string loginSql = "select UserName from UserList where username=@UserName and password=@PassWord";
SqlConnection conn = new SqlConnection(connStr);
//SqlDataAdapter da = new SqlDataAdapter(loginSql, conn);
//DataSet ds = new DataSet();
//da.Fill(ds);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
if (txtUserName.Text.Length == 0)
{
MessageBox.Show("用户不能为空,请查询后输入");
txtPassWord.Focus();
return;
}
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtUserName.Text + "'";
conn.Open();
if ((int)cmd.ExecuteScalar() == 0)
{
MessageBox.Show("不存在此用户,请检查后重新输入");
txtPassWord.Focus();
return;
}
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text + "'";
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
MessageBox.Show("登录成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("用户" + txtUserName .Text + "的密码不正确", "密码", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
txtPassWord.Focus();
return;
}
问题1:运行后出现错误1 if ((int)cmd.ExecuteScalar() == 0) 这个有错误?
问题2: SqlDataReader myReader = cmd.ExecuteReader(); 错误??
请高手分析解答!
string username = txtUserName.Text.ToString();
string connStr = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=DBApp";
// string loginSql = "select UserName from UserList where username=@UserName and password=@PassWord";
SqlConnection conn = new SqlConnection(connStr);
//SqlDataAdapter da = new SqlDataAdapter(loginSql, conn);
//DataSet ds = new DataSet();
//da.Fill(ds);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
if (txtUserName.Text.Length == 0)
{
MessageBox.Show("用户不能为空,请查询后输入");
txtPassWord.Focus();
return;
}
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtUserName.Text + "'";
conn.Open();
if ((int)cmd.ExecuteScalar() == 0)
{
MessageBox.Show("不存在此用户,请检查后重新输入");
txtPassWord.Focus();
return;
}
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text + "'";
SqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
MessageBox.Show("登录成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("用户" + txtUserName .Text + "的密码不正确", "密码", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
txtPassWord.Focus();
return;
}
问题1:运行后出现错误1 if ((int)cmd.ExecuteScalar() == 0) 这个有错误?
问题2: SqlDataReader myReader = cmd.ExecuteReader(); 错误??
请高手分析解答!
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text + "'";
cmd.CommandText = "select count(*) from UserList where UserName=='" + txtPassWord.Text + "'";
以改正不过还是出现错误: if ((int)cmd.ExecuteScalar() == 0)附近有错误!
楼主把
sql=和c#==混了吧