Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'数据库连接
Dim conn As New SqlClient.SqlConnection
'conn的连接字符串在下边的case语句中进行定义
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = conn
'定义变量
Dim strpassword As String
Dim strRealPw As String
Dim strTableName As String = DropDownList1.SelectedItem.value '用户登陆表名
Dim strID As String '用户ID字符串
Dim str_PW As String '用户密码字符串
Dim strPath As String '根据用户身份登陆字符串
Dim strUserIdent As String '用户身份标识
Dim bool1 As Boolean = False '判断是否通过验证
Dim strupdate As String '更新语句
Dim str_iflogin As String
'数据库查询语句---在此处添加语句--对用户分类——设置用户ID字符串
Dim flag As Boolean = True
Select Case strTableName
Case "student_login"
conn.ConnectionString = ConfigurationSettings.AppSettings("DSN_student")
strID = "S_ID"
str_PW = "SL_pwd"
'str_iflogin = "sl_if"
strPath = "../network_course/student/index.aspx"
strUserIdent = "同学"
Case "teacher_login"
conn.ConnectionString = ConfigurationSettings.AppSettings("DSN_teacher")
strID = "T_ID"
str_PW = "TL_pwd"
'str_iflogin = "tl_if"
strPath = "../network_course/teacher/index.aspx"
strUserIdent = "老师"
End Select
'验证用户ID和密码
cmd.CommandText = "select * from " + strTableName + " where " + strID + "='" & TextBox1.Text & "'" '查询语句
Try
conn.Open()
Dim reader As IDataReader
reader = cmd.ExecuteReader()
strpassword = TextBox2.Text
If reader.Read Then
strRealPw = reader.Item(str_PW)
If Trim(strpassword) = Trim(strRealPw) Then '比较密码和输入
'If reader.Item(str_iflogin) = 0 Then
bool1 = True
'Else
' Label4.Text = "此账户已经登录在线!"
'End If
Else
Label4.Text = "密码错误!"
bool1 = False
End If
Else
Label4.Text = "用户名输入错误!"
bool1 = False
End If
reader.Close()
Catch
Label4.Text = "数据库操作失败,可能是服务器出现错误,请稍后再试!非常抱歉影响了您的工作!"
Finally
conn.Close()
strpassword = ""
strRealPw = ""
End Try
If bool1 Then '
'设置cookie参数
Dim userName As String
Dim userIdentity As String
Dim userRealName As String
If (Request.Cookies("userInf")) Is Nothing Then
Dim cookie As HttpCookie = New HttpCookie("userInf") '创建新的cookie
cookie.values.add("userID", TextBox1.Text) '加入用户的账号
cookie.values.add("userIdent", strUserIdent)
Response.AppendCookie(cookie)
Else
Dim cookie As HttpCookie = New HttpCookie("userInf")
cookie.values("userID") = TextBox1.Text
cookie.values("userIdent") = strUserIdent
Response.AppendCookie(cookie)
End If
Response.Redirect(strPath) '进入到主页面
End If
End Sub
End Class