簡體   English   中英

ASP.NET-VB.NET-從MS-Access數據庫檢索記錄

[英]ASP.NET - VB.NET - Retrieving record from MS-Access database

我正在嘗試從MS-Access數據庫檢索記錄數據並將其存儲到變量中。

我正在使用以下SQL命令來查詢數據庫:

Dim cmdRead As OleDbCommand = New OleDbCommand("SELECT UserID FROM [User] where Username=?", conn)

如何在VB.NET中做到這一點,以便從UserID列(在數據庫中)讀取數據並將其存儲到VB.NET中的變量中?

我基本上想要做的是檢索UserID,以便隨后可以從同一表中檢索用戶的名稱和姓氏。

注意:我正在將ASP.NET與Web Develop 2003和MS-Access 2003數據庫一起使用。

這是完整的代碼:

  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

您的示例出了點問題。 請嘗試這個。

首先將OleDbCommand更改為還檢索用戶ID,當您從同一表中讀取數據時不需要第二個命令,並且我想UserName和Password可以標識唯一的用戶

Dim cmd As OleDbCommand = New OleDbCommand("SELECT UserID, Username, Password FROM [User] where Username=? and Password=?", conn) 

然后添加您的參數並查詢數據庫

Try             
     conn.Open()
     Dim read As OleDbDataReader = cmd.ExecuteReader()             
     If read.HasRows Then              
         read.Read()
         ' Now you can read the Item and pass the values to your textboxes             
         txtUsername.Text = read.Item("username").ToString 
         txtPassword.Text = read.Item("password").ToString 
         txtUserID.Text = read.Item("userid").ToString  
     Else                 
          lblLogged.Text = "Login unsuccessful"             
     End If              
     read.Close()         
Catch ex As Exception             
     Response.Write(ex.Message())          
Finally             
     conn.Close()         
 End Try 

這應該在您的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
        Else                 
            lblLogged.Text = "Login unsuccessful"             
        End If   
    End While           
End If   

然后在Final塊中

read.Close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM