ConnectionClass objconn=new ConnectionClass();//实例化一个连接类
SqlConnection logConn = objconn.GetConnection();
DataSet logDs = new DataSet();//实例化一个数据集
try
......{
SqlCommand logCmd = logConn.CreateCommand(); //定义命令变量
logCmd.CommandType = CommandType.StoredProcedure;//指定命令变量的类型
logCmd.CommandText = "LoginProc";//指定命令变量的执行语句为存储过程名
logCmd.Parameters.Add(new SqlParameter("@Id", txtId.Text.Trim().ToString()));//添加参数
logCmd.Parameters.Add(new SqlParameter("@pwd", txtPassword.Text.Trim().GetHashCode().ToString()));//添加参数
//注意:参数名 和个数必须与存储过程的参数一样
SqlDataAdapter logSda = new SqlDataAdapter(logCmd);//定义适配器
logSda.Fill(logDs, "userinfo");//填充适配器
if (logDs.Tables["userinfo"].Rows.Count > 0)
......{
string objui="";
objui=txtId.Text.Trim().ToString();
Session.Add("wid",objui);//写入Session
//Response.Write("<script language='JavaScript'>alert('用户名、密码 正确 !')</script>");
Response.Redirect("mainFrame/mainframe.htm" ); //转向页面
}
else
......{
Response.Write("<script language='JavaScript'>alert('用户不存在 或 密码不正确 !')</script>");
return;
}
}
catch (Exception ex)
......{
Response.Write("<script language='JavaScript'>alert('登录失败 !')</script>");
return;
}
finally
......{
logConn.Close();//断开连接
}
本文介绍了一种使用SQL存储过程实现用户登录验证的方法。通过创建存储过程并传递用户名和经过哈希处理的密码作为参数,可以在数据库层面高效地完成用户身份验证。文章详细展示了如何在.NET框架下建立数据库连接、调用存储过程及处理返回结果。

被折叠的 条评论
为什么被折叠?



