问候男人和女孩,
我最近才刚刚开始在ASP.NET和VB.NET中进行编程,因此我一直在研究语法和逻辑。无论如何,我有一个SQL查询,该查询根据输入的用户名获取用户的详细信息。我的问题是:由于我从数据库表中进行了SELECT *,我该如何将这些值放入变量中?我想保存到变量的值是ID,Firstname和Lastname。我的计划是保存登录的当前用户ID,这样它将在我的应用程序中全部显示。
这就是我所拥有的:
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim strconn As New SqlConnection
If strconn.State = ConnectionState.Closed Then
strconn.ConnectionString = ("Server=0.0.0.0;Database=Test;Uid=test;Pwd=test;")
End If
'//prompt user for blank user credentials
If txtUsername.Text.Length = 0 Then
Response.Write("<SCRIPT LANGUAGE=""JavaScript""> alert('You must enter your Access Credentials.');</script>")
'If MsgBox("You must enter your Access Credentials.", vbInformation, vbOKCancel) Then
'End If
txtUsername.Focus()
Exit Sub
End If
Try
strconn.Open()
Dim sQuery As String = "SELECT * from tblUsers WHERE username = '" & txtUsername.Text & "'" &
" AND password = '" & txtPassword.Text & "'" &
" AND status ='" & "1" & "'"
Dim datareader As SqlDataReader
Dim adapter As New SqlDataAdapter
Dim parameter As New SqlParameter
Dim command As SqlCommand = New SqlCommand(sQuery, strconn)
command.Connection = strconn
adapter.SelectCommand = command
datareader = command.ExecuteReader()
If datareader.HasRows Then
datareader.Read()
'MsgBox("Login Successfull! ", vbInformation, vbOKOnly)
'Response.Redirect("http://localhost:0000/SecurityStaffDataEntry.aspx")
Response.Redirect("http://localhost:0000/index.aspx")
datareader.Close()
LoginOk = True
Else
Response.Write("<SCRIPT LANGUAGE=""JavaScript""> alert('Invalid Username or Password!');</script>")
' MsgBox("Invalid Username or Password! ", vbInformation, vbOKOnly)
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()
End If
datareader.Close()
Catch ex As Exception
'MsgBox(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
将其放在reader.Read()之后,并用变量替换文本框
while (reader.Read())
{
TextBox1.Text = reader[0].ToString();
}
您也可以使用
reader["columnname"].ToString()