【发布时间】:2012-03-26 15:05:15
【问题描述】:
我正在尝试从 MS-Access 数据库中检索记录数据并将其存储到变量中。
我正在使用这个 SQL 命令来查询数据库:
Dim cmdRead As OleDbCommand = New OleDbCommand("SELECT UserID FROM [User] where Username=?", conn)
如何在 VB.NET 中从 UserID 列(在 db 中)读取数据并将其存储到 VB.NET 中的变量中?
我基本上想要做的是检索用户 ID,以便我可以从同一个表中检索用户的姓名和姓氏。
注意:我将 ASP.NET 与 Web Develop 2003 和 MS-Access 2003 db 一起使用。
这是完整的代码:
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT Username, Password FROM [User] where Username=? and Password=?", conn)
cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
Try
conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
While read.Read()
If txtUsername.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then
Dim tID As Long = read.Item("UserID").ToString
Dim tName As String = read.Item("CustomerName").ToString
Dim tSurname As String = read.Item("CustomerSurname").ToString
lblLogged.Text = lblLogged.Text + tName + tSurname
lblLogged.Visible = True
End If
End While
Else
lblLogged.Text = "Login unsuccessful"
End If
read.Close()
Catch ex As Exception
Response.Write(ex.Message())
Finally
conn.Close()
End Try
End Sub
End Class
【问题讨论】:
-
您目前是否遇到错误?
-
不,不。它工作正常。我想要的是添加代码来检索用户 ID、姓名和姓氏,而不是插入。
标签: asp.net sql vb.net ms-access