vb 登录成功后直接将sql数据库中的数据放入文本框
vb Directly putting sql data from database into textbox once login is sucessful
我需要一些帮助,以便在登录成功后将数据库中的 SQL 数据直接放入 Textbox1(名字)和 Textbox2(姓氏)中。基本上,我的代码会验证用户名、密码和电子邮件(使用我的 sql table 中存储的密码、用户名和电子邮件)。然后,它应该 将附加到用户名、密码和电子邮件的名字和姓氏放入 textbox1 和 textbox2。但是,我尝试过的所有教程都使用下拉列表。有人可以告诉我我需要什么才能正确获取我的那部分代码 运行 吗?这是我的第一次尝试。这是我的代码:
Protected Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If TextBox7.Text = Nothing Then
MsgBox(“Please enter Username”, vbExclamation, “Error”)
Exit Sub
End If
If TextBox8.Text = Nothing Then
MsgBox(“Please enter Password”, vbExclamation, “Error”)
Exit Sub
End If
If TextBox9.Text = Nothing Then
MsgBox(“Please enter Email”, vbExclamation, “Error”)
Exit Sub
End If
Dim un, pw, em, dbUN, dbPW, dbEM As String
un = TextBox7.Text
pw = TextBox8.Text
em = TextBox9.Text
Dim cmdUN As New SqlCommand("Select UserName from MembershipInfo where UserName = @p1", con)
With cmdUN.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdPW As New SqlCommand("Select Password from MembershipInfo where UserName = @p1", con)
With cmdPW.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdEM As New SqlCommand("Select Email from MembershipInfo where UserName = @p1", con)
With cmdEM.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdPUN As New SqlCommand("Select Firstname, Lastname From MembershipInfo where Username = @p1, Password = @p2, Email = @p3")
Dim myreader As SqlDataReader
With cmdPUN.Parameters
.Clear()
.AddWithValue("@p1", un)
.AddWithValue("@p2", pw)
.AddWithValue("@p3", em)
End With
Try
If con.State = ConnectionState.Closed Then con.Open()
dbUN = cmdUN.ExecuteScalar
dbPW = cmdPW.ExecuteScalar
dbEM = cmdEM.ExecuteScalar
myreader = cmdPUN.ExecuteReader()
myreader.Read()
If myreader.HasRows Then
TextBox1.Text = myreader.Item("Firstname").ToString
TextBox2.Text = myreader.Item("Lastname").ToString
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
con.Close()
End Try
If (un = dbUN And pw = dbPW And em = dbEM) Then
MsgBox("Login Sucessful", vbExclamation, "Welcome")
Else
If un <> dbUN Then
MsgBox("Username does not match, please try again", vbExclamation, "Error")
Else
If pw <> dbPW Then
MsgBox("Password does not match, please try again", vbExclamation, "Error")
Else
If em <> dbEM Then
MsgBox("Email does not match, please try again", vbExclamation, "Error")
End If
End If
End If
End If
TextBox7.Text = String.Empty
TextBox8.Text = String.Empty
TextBox9.Text = String.Empty
End Sub
我原以为电子邮件可以唯一标识您的用户,而用户名是不必要的。您永远不应将密码存储为纯文本。我在上次给你的回答中已经给出了解释。希望你回去看看。我给了你的控件描述性名称,我建议你也这样做。
Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
If Not ValidateInput() Then
Exit Sub
End If
Dim dt As DataTable
Try
dt = ValidateUser(txtUserName.Text, txtEmail.Text, txtPassword.Text)
Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try
If dt.Rows.Count > 0 Then
txtFirstName.Text = dt(0)("Firstname").ToString
txtLastName.Text = dt(0)("Lastname").ToString
MsgBox("Login Sucessful", vbExclamation, "Welcome")
txtUserName.Text = String.Empty
txtEmail.Text = String.Empty
txtPassword.Text = String.Empty
End If
End Sub
Private Function ValidateInput() As Boolean
If txtUserName.Text = Nothing Then
MsgBox(“Please enter Username”, vbExclamation, “Error”)
Return False
End If
If txtEmail.Text = Nothing Then
MsgBox(“Please enter Email”, vbExclamation, “Error”)
Return False
End If
If txtPassword.Text = Nothing Then
MsgBox(“Please enter Password”, vbExclamation, “Error”)
Return False
End If
Return True
End Function
Private Function ValidateUser(UName As String, Email As String, PWord As String) As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection("Your connection string."),
cmdUN As New SqlCommand("Select FirstName, LastName from MembershipInfo where UserName = @User And Email = @Email And Password = @Password", cn)
cmdUN.Parameters.Add("@User", SqlDbType.VarChar).Value = UName
cmdUN.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email
cmdUN.Parameters.Add("@Password", SqlDbType.VarChar).Value = PWord
cn.Open()
Using reader = cmdUN.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function
我需要一些帮助,以便在登录成功后将数据库中的 SQL 数据直接放入 Textbox1(名字)和 Textbox2(姓氏)中。基本上,我的代码会验证用户名、密码和电子邮件(使用我的 sql table 中存储的密码、用户名和电子邮件)。然后,它应该 将附加到用户名、密码和电子邮件的名字和姓氏放入 textbox1 和 textbox2。但是,我尝试过的所有教程都使用下拉列表。有人可以告诉我我需要什么才能正确获取我的那部分代码 运行 吗?这是我的第一次尝试。这是我的代码:
Protected Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If TextBox7.Text = Nothing Then
MsgBox(“Please enter Username”, vbExclamation, “Error”)
Exit Sub
End If
If TextBox8.Text = Nothing Then
MsgBox(“Please enter Password”, vbExclamation, “Error”)
Exit Sub
End If
If TextBox9.Text = Nothing Then
MsgBox(“Please enter Email”, vbExclamation, “Error”)
Exit Sub
End If
Dim un, pw, em, dbUN, dbPW, dbEM As String
un = TextBox7.Text
pw = TextBox8.Text
em = TextBox9.Text
Dim cmdUN As New SqlCommand("Select UserName from MembershipInfo where UserName = @p1", con)
With cmdUN.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdPW As New SqlCommand("Select Password from MembershipInfo where UserName = @p1", con)
With cmdPW.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdEM As New SqlCommand("Select Email from MembershipInfo where UserName = @p1", con)
With cmdEM.Parameters
.Clear()
.AddWithValue("@p1", un)
End With
Dim cmdPUN As New SqlCommand("Select Firstname, Lastname From MembershipInfo where Username = @p1, Password = @p2, Email = @p3")
Dim myreader As SqlDataReader
With cmdPUN.Parameters
.Clear()
.AddWithValue("@p1", un)
.AddWithValue("@p2", pw)
.AddWithValue("@p3", em)
End With
Try
If con.State = ConnectionState.Closed Then con.Open()
dbUN = cmdUN.ExecuteScalar
dbPW = cmdPW.ExecuteScalar
dbEM = cmdEM.ExecuteScalar
myreader = cmdPUN.ExecuteReader()
myreader.Read()
If myreader.HasRows Then
TextBox1.Text = myreader.Item("Firstname").ToString
TextBox2.Text = myreader.Item("Lastname").ToString
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
con.Close()
End Try
If (un = dbUN And pw = dbPW And em = dbEM) Then
MsgBox("Login Sucessful", vbExclamation, "Welcome")
Else
If un <> dbUN Then
MsgBox("Username does not match, please try again", vbExclamation, "Error")
Else
If pw <> dbPW Then
MsgBox("Password does not match, please try again", vbExclamation, "Error")
Else
If em <> dbEM Then
MsgBox("Email does not match, please try again", vbExclamation, "Error")
End If
End If
End If
End If
TextBox7.Text = String.Empty
TextBox8.Text = String.Empty
TextBox9.Text = String.Empty
End Sub
我原以为电子邮件可以唯一标识您的用户,而用户名是不必要的。您永远不应将密码存储为纯文本。我在上次给你的回答中已经给出了解释。希望你回去看看。我给了你的控件描述性名称,我建议你也这样做。
Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
If Not ValidateInput() Then
Exit Sub
End If
Dim dt As DataTable
Try
dt = ValidateUser(txtUserName.Text, txtEmail.Text, txtPassword.Text)
Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try
If dt.Rows.Count > 0 Then
txtFirstName.Text = dt(0)("Firstname").ToString
txtLastName.Text = dt(0)("Lastname").ToString
MsgBox("Login Sucessful", vbExclamation, "Welcome")
txtUserName.Text = String.Empty
txtEmail.Text = String.Empty
txtPassword.Text = String.Empty
End If
End Sub
Private Function ValidateInput() As Boolean
If txtUserName.Text = Nothing Then
MsgBox(“Please enter Username”, vbExclamation, “Error”)
Return False
End If
If txtEmail.Text = Nothing Then
MsgBox(“Please enter Email”, vbExclamation, “Error”)
Return False
End If
If txtPassword.Text = Nothing Then
MsgBox(“Please enter Password”, vbExclamation, “Error”)
Return False
End If
Return True
End Function
Private Function ValidateUser(UName As String, Email As String, PWord As String) As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection("Your connection string."),
cmdUN As New SqlCommand("Select FirstName, LastName from MembershipInfo where UserName = @User And Email = @Email And Password = @Password", cn)
cmdUN.Parameters.Add("@User", SqlDbType.VarChar).Value = UName
cmdUN.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email
cmdUN.Parameters.Add("@Password", SqlDbType.VarChar).Value = PWord
cn.Open()
Using reader = cmdUN.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function