在 vb.net 中使用 sql 查询检索访问数据 [英] retrieve access data using sql query in vb.net

查看:49
本文介绍了在 vb.net 中使用 sql 查询检索访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 vb.net 中做一个项目,并使用 access 2003 作为后端.我想要某种方式来获取用户 ID 为uid"的某个特定用户的密码pwd".

i am doing a project in vb.net, and using access 2003 as back end. i want some way of obtaining the password "pwd" of some particular user having user id as "uid".

使用 ComboBox 获取用户 ID

the user id is obtained using a ComboBox

推荐答案

在我们开始之前,您必须知道user"是一个保留字.你不应该用保留字来管理一个表.它会导致各种问题.但是,我们可以使用括号来处理.

Before we begin, you must be aware that "user" is a reserved word. You should not mane a table with a reserved word. It causes all sorts of problems. However, we can deal with that using brackets.

有很多方法可以按照您的要求去做,但这就是我通常的处理方式.

There are many ways to do what you ask, but this is how I usually handle it.

首先,您需要一个数据连接.我不确定您使用的是什么 CPU 平台,如果它是 64 位处理器,您将需要进行配置 - 并将活动解决方案平台更改为x86".

First, you need a data connection. I'm not sure what CPU platform you are using, if it is a 64bit processor, you will need to go to configuration - and change the Active Solution Platform to "x86".

其次,您需要一个数据集来保存数据.用数据集中的数据填充组合框.

Second, you need a datset to hold the data. Fill the combobox with data from the dataset.

从组合框中选择uid"后,您将需要一种仅找到该用户的方法.我建议使用数据视图.

Aftr selecting the "uid" from the combobox, you will need a way to find only that user. I suggest a dataview.

这是一个完成任务的示例程序.我试过了,确实有效.

This a sample program to accomplish the task. I have tried it and it does work.

Public Class Form1

Dim oleConn As New OleDb.OleDbConnection
Dim sql As String
Dim command As New OleDb.OleDbCommand
Dim oleAdapter As New OleDb.OleDbDataAdapter
Dim UserDataSet As New DataSet
Dim usid As String
Dim x As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    oleConn = New OleDb.OleDbConnection
    oleConn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=<< DATBASE PATH>>"
    sql = "Select * From [User]"
    oleConn.Open()
    command = New OleDb.OleDbCommand(sql, oleConn)
    command.ExecuteNonQuery()
    oleAdapter = New OleDb.OleDbDataAdapter("Select * From [User]", oleConn)
    UserDataSet = New DataSet
    oleAdapter.Fill(UserDataSet)
    oleConn.Dispose()
    cmbUid.DataSource = UserDataSet.Tables(0)
    cmbUid.DisplayMember = "uid"
End Sub
Private Sub cmbUid_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbUid.SelectedValueChanged
    usid = cmbUid.Text
    FindPWD()
End Sub
Private Sub FindPWD()
    'Create a dataview
    Dim dv As New DataView
    'Associate the dataview to _oDs (Dataset table)
    dv.Table = UserDataSet.Tables(0)
    Dim drv As DataRowView 'Data Row View object to query DataView object
    'Filter based on a combo box value selected
    dv.RowFilter = "[uid] LIKE '" & usid & "'"
    'Retrieve my values returned in the result
    For Each drv In dv
        'clear  textbox first time thru
        If x = 0 Then
            x = 1
            txtPwd.Clear()
        Else
            txtPwd.Text = drv("pwd")
        End If
    Next

End Sub

End Class

这篇关于在 vb.net 中使用 sql 查询检索访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆