用户身份验证

来源:百度知道 编辑:UC知道 时间:2024/06/06 01:48:45
try
{
string strSql = "select 用户名,密码 from [VIP].[dbo].[管理员用户表]"
+ "where @用户名=textBox2 .Text .Trim ()" + "and @密码 = textBox1 .Text .Trim ()";
cmd .Connection =con ;
cmd .CommandText =strSql ;

if (strSql = true) //此行
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex .Message );
}

上面是登陆界面的用户身份验证,使用sql语句查询数据库中的 用户名和密码,如果与文本框中输入的相同那么进入下一个窗体,否则报错。我请问大家我的SQL有错误吗?进入下一个窗体是使用if语句吗,如果是该怎么写???

完全错了。

SqlConnection conn = new SqlConncection(你的连接字串);
conn.Open();

string strSql = "select 用户名,密码 from [VIP].[dbo].[管理员用户表]"
+ "where 用户名='" + textBox2 .Text .Trim () + "' and 密码 = '" + textBox1 .Text .Trim ()+"'";

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strSql;
SqlDataReader rd = cmd.ExecuteReader();
if (!rd.Read())
{
MessageBox.Show("无此用户或是密码错!");
return;
}
conn.Close();
Form2 form2 = new Form2();
form2.ShowDialog();

基上就是这样了。好好调试一下。

string strSql = "select count(*) from [管理员用户表]"
+ " where 用户名='"+textBox2.Text.Trim ()+"' and 密码='"+ textBox1.Text.Trim ()+"'";
cmd .Connection =con ;
cmd .CommandText =strSql ;
con.Open();