本文介绍了在代码中需要帮助来检查用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我写了一个选择查询来从数据库中获取用户名和密码



现在我想检查用户名和密码是否正确,如果是正确的话会做一些操作



请告诉我怎么写代码



谢谢你

Hi all

I wrote a select query to get the username and password from database

now i want to check whether the username and password is correct or not and if it is correct then will do some operation

please tell me how to write the code

Thank you

推荐答案

Sub validateuser()
       Try
           sqlcon = New OleDbConnection(constr)
           sqlcon.Open()
           sqlcmd = New OleDbCommand("SELECT COUNT(*) FROM att_userrole WHERE username = '" + Login1.UserName + " AND password = '" + Login1.Password + "'", sqlcon)
           retval = sqlcmd.ExecuteScalar
           If retval = 1 Then
               sqlcmd = New OleDbCommand("SELECT role, emailid FROM att_userrole WHERE username = '" + Login1.UserName + "'", sqlcon)
               sqlrdr = sqlcmd.ExecuteReader()
               sqlrdr.Read()
               Session("username") = Login1.UserName
               getrole = (sqlrdr.Item("role"))
               Session("userrole") = getrole
               getemail = (sqlrdr.Item("emailid"))
               Session("reqemailid") = getemail
               Response.Redirect("~\Home.aspx")
               'Response.Write("you are valid " & Session("username") & " with role " & getrole)
           Else
               'IF USERNAME IS INVALID
               '-----YOUR CODE GOES HERE----
              lbl_msg.Text = ("Invalid login attmept")

           End If
       Catch ex As Exception
           lbl_msg.Text = ex.Message.ToString
       End Try
   End Sub





希望这有帮助



Hope this helps


SELECT
  Count(ID)
FROM
  MstUsers
WHERE
  Username = @username AND
  Password = @password



如果计数大于零,您就知道组合是正确的。如果需要,您可以检索除计数之外的任何其他信息。


If the count is greater then zero, you know the combination is correct. You can retrieve any other information other then count if you need.


这篇关于在代码中需要帮助来检查用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:22