当前位置: 代码迷 >> ASP.NET >> 使用datareader判断用户登录是否成功,该怎么处理
  详细解决方案

使用datareader判断用户登录是否成功,该怎么处理

热度:2766   发布时间:2013-02-25 00:00:00.0
使用datareader判断用户登录是否成功
不要求复杂功能,已经建好一个数据库,连接也已完成,
SqlConnection comn = new SqlConnection("Data Source=.;Initial Catalog=userinformation;Integrated Security=True");
  comn.Open();
  SqlCommand command1 = new SqlCommand("select * from userinformation", comn);
  SqlDataReader reader1 = command1.ExecuteReader();
代码写到这儿,接下来该怎么写,目的是和数据库中的信息比对,如果用户名密码均正确则返回登陆成功,只要有一个不正确则返回用户名和密码不匹配,我刚开始学,希望各位高手赐教

------解决方案--------------------------------------------------------
原理就是遍历,然后匹配。
但是要注意防注入。
------解决方案--------------------------------------------------------
C# code
string uname = "XX";string pwd = "ZZ";using(SqlConnection comn = new SqlConnection("Data Source=.;Initial Catalog=userinformation;Integrated Security=True")){   comn.Open();   SqlCommand command1 = new SqlCommand("select * from userinformation where username='"+uname+"'", comn);   SqlDataReader reader1 = command1.ExecuteReader();   if(reader1.Read())   {      if(reader1["password"].Equals(pwd))      {          Response.Write("<script>alert('登录验证正确!');</script>");      }      else      {         Response.Write("<script>alert('密码错误!');</script>");      }   }   else   {      Response.Write("<script>alert('用户名错误!');</script>");   }}