如何从结果集中获取特定列的值 [英] How to get the value of a particular column from a result set
问题描述
我在Visual Studio 2010中使用带有Web表单的visual basic使用asp.net 4.0代码。
我有以下代码:
表示层:
< %@ Page < span class =code-attribute>标题 = 语言 = VB MasterPageFile = 〜/ MasterPage.master AutoEventWireup = false CodeFile = Login.aspx.vb 继承 = 登录 %> ;
< asp:Content ID = Content1 ContentPlaceHolderID = head Runat = 服务器 >
< / asp:Content >
< asp:Content ID = Content2 ContentPlaceHolderID = ContentPlaceHolder1 Runat < span class =code-keyword> = 服务器 >
< br / > &l t; br / >
< asp:Label ID = lblUsername runat = 服务器 正文 = 输入用户名 > < / asp:Label >
< asp:TextBox ID = txtUsername runat = server AutoCompleteType < span class =code-keyword> = 已停用 > < / asp:TextBox >
< asp:RequiredFieldValidator ID = RequiredFieldValidator1 runat = server
ControlToValidate = txtUsername ErrorMessage = RequiredFieldValidator
ForeColor = 红色 > * < / asp:RequiredFieldValidator >
< br / > < ; br / < span class =code-keyword>>
< asp:Label ID = lblPassword runat = server 文本 = 输入密码 > < / asp:Label >
< asp:TextBox < span class =code-attribute> ID = txtPassword runat = server TextMode = 密码 > < / asp:TextBox >
< asp:RequiredFieldValidator ID = RequiredFieldValidator2 runat = server
ControlToValidate = txtPassword ErrorMessage = RequiredFieldValidator
ForeColor = 红色 > * < / asp:RequiredFieldValidator >
< br / > < br / >
选择帐户类型
< asp:DropDownList ID = ddlAccountType runat = server >
< asp:ListItem 值 = C > 客户< / asp :ListItem >
< asp:ListItem 值 = S > 卖方< / asp:ListItem >
< / asp:DropDownList >
< br / > < br / >
< asp:Label ID = lblRememberMe runat < span class =code-keyword> = server 文本 = 记住我 > < / asp:标签 >
< asp:CheckBox ID = chkRememberMe runat = server / >
< br / > ; < br / >
< asp:按钮 ID = btnLogin runat = server 文字 = 登录 / >
< / asp:Content >
代码隐藏:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class Login
Inherits System.Web.UI.Page
Private ReadOnly _conString As String
Public Sub New()
_conString =
WebConfigurationManager.ConnectionStrings(PetiteAnnonceCS)。ConnectionString
End Sub
Protected Sub btnLogin_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理btnLogin.Click
Dim con2 As New SqlConnection(_conString)
Dim sql2 As String =SELECT UserId FROM tblUser WHERE用户名= @用户名和密码= @密码
Dim c md2 As New SqlCommand(sql2,con2)
cmd2.Parameters.AddWithValue(@ Username,txtUsername.Text)
cmd2.Parameters.AddWithValue(@ Password,txtPassword.Text)
Dim myReader As SqlDataReader
使用con2
con2.Open()
myReader = cmd2.ExecuteReader
如果myReader.HasRows那么
如果chkRememberMe.Checked = True则
Session(Username)= txtUsername.Text
Session(LoginTime)= DateAndTime.Now.ToString
Session.Timeout = 50000
如果ddlAccountType.SelectedValue =C那么
Response.Redirect(〜/ Customer.aspx)
否则:Response.Redirect(〜/ Seller.aspx)
结束如果
Else
Session(Username)= txtUsername.Text
Session(LoginTime)= Da teAndTime.Now.ToString
如果ddlAccountType.SelectedValue =C那么
Response.Redirect(〜/ Customer.aspx)
否则:Response.Redirect(〜/ Seller.aspx )
结束如果
结束如果
否则
MsgBox(登录UnSuccessful!)
结束如果
con2.Close()
结束使用
结束次级
结束等级
我有UserId为我的表中的主键User。
当用户登录时,系统会检查用户是否有效,如果有效,则用户名存储在会话变量中。在会话中存储用户名很容易,因为我们可以轻松地从文本框中获取值,但我也想获取UserId值并将其存储在会话变量中,我对此没有任何线索。有人可以帮忙吗?
有没有办法知道用户
if myReader.HasRows Then
Session( UserID)= reader.GetString( 0 )
参考:使用DataReader检索数据 [ ^ ]
你可以使用 ExecuteScalar方法 [ ^ ]因为您需要一个值。阅读文档和给出的样本。
最后我终于解决了它,但不幸的是,在我提交课程后,解决方案非常简单,我很困惑你们怎么都无法想象它出来了。
Dim con 作为 新 SqlConnection(_conString)
Dim sql 作为 字符串 = SELECT AccountType FROM tblUser WHERE Username = @ Username
Dim cmd As 新 SqlCommand(sql,con)
cmd.Parameters.AddWithValue( @用户名跨度>, txtUsername.Text)
Dim myReader As SqlDataReader
con.Open()
myReader = cmd.ExecuteReader
DetailsView1.DataSource = myReader
DetailsView1.DataBind()
' 从查询返回的行中读取accountType字段并将其存储在字符串中
Dim AT As String = myReader( AccountType)
如果 AT.Contains( < span class =code-string> Customer)然后
Resp onse.Redirect( 〜/ Customer.aspx)
Else :Response.Redirect( 〜/ Seller.aspx)
I'm using asp.net 4.0 code using visual basic in visual studio 2010 with web forms.
I have the following code:
Presentation layer:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br /><br />
<asp:Label ID="lblUsername" runat="server" Text="Enter Username"></asp:Label>
<asp:TextBox ID="txtUsername" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUsername" ErrorMessage="RequiredFieldValidator"
ForeColor="Red">*</asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="lblPassword" runat="server" Text="Enter Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPassword" ErrorMessage="RequiredFieldValidator"
ForeColor="Red">*</asp:RequiredFieldValidator>
<br /><br />
Select Account Type
<asp:DropDownList ID="ddlAccountType" runat="server">
<asp:ListItem Value="C">Customer</asp:ListItem>
<asp:ListItem Value="S">Seller</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Label ID="lblRememberMe" runat="server" Text="Remember me"></asp:Label>
<asp:CheckBox ID="chkRememberMe" runat="server" />
<br /><br />
<asp:Button ID="btnLogin" runat="server" Text="Login" />
</asp:Content>
code-behind:
Imports System.Data Imports System.Data.SqlClient Imports System.Web.Configuration Partial Class Login Inherits System.Web.UI.Page Private ReadOnly _conString As String Public Sub New() _conString = WebConfigurationManager.ConnectionStrings("PetiteAnnonceCS").ConnectionString End Sub Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click Dim con2 As New SqlConnection(_conString) Dim sql2 As String = "SELECT UserId FROM tblUser WHERE Username=@Username And Password=@Password" Dim cmd2 As New SqlCommand(sql2, con2) cmd2.Parameters.AddWithValue("@Username", txtUsername.Text) cmd2.Parameters.AddWithValue("@Password", txtPassword.Text) Dim myReader As SqlDataReader Using con2 con2.Open() myReader = cmd2.ExecuteReader If myReader.HasRows Then If chkRememberMe.Checked = True Then Session("Username") = txtUsername.Text Session("LoginTime") = DateAndTime.Now.ToString Session.Timeout = 50000 If ddlAccountType.SelectedValue = "C" Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx") End If Else Session("Username") = txtUsername.Text Session("LoginTime") = DateAndTime.Now.ToString If ddlAccountType.SelectedValue = "C" Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx") End If End If Else MsgBox("Login UnSuccessful!") End If con2.Close() End Using End Sub End Class
I have the UserId as the primary key in my table User.
When the user logs in, the system checks if the user is valid, if it's valid the username is stored in a session variable. It's easy to store the username in a session because we can easily grab the value from the textbox, but I want to grab the UserId value also and store it in a session variable which I don't have any clue on how to do it. Can anyone help?
Is there a way to know the user
If myReader.HasRows Then Session("UserID") = reader.GetString(0)
refer : Retrieving Data Using a DataReader[^]
you can use ExecuteScalar Method[^] since you need one value. Read the documentation and the samples given.
Finally I've finally solved it but unfortunately after I submitted my coursework, the solution is pretty straightforward, I am puzzled on how you guys could not figure it out.
Dim con As New SqlConnection(_conString) Dim sql As String = "SELECT AccountType FROM tblUser WHERE Username=@Username" Dim cmd As New SqlCommand(sql, con) cmd.Parameters.AddWithValue("@Username", txtUsername.Text) Dim myReader As SqlDataReader con.Open() myReader = cmd.ExecuteReader DetailsView1.DataSource = myReader DetailsView1.DataBind() 'Read the accountType field from the row returned from the query and store it in a string Dim AT As String = myReader("AccountType") If AT.Contains("Customer") Then Response.Redirect("~/Customer.aspx") Else : Response.Redirect("~/Seller.aspx")
这篇关于如何从结果集中获取特定列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!