vb.net 当用户登录程序时,如何从 Access 数据库中检索数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22855434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:18:05  来源:igfitidea点击:

How to retrieve data from Access Database, when user are login to the program?

vb.netms-accessvisual-studio-2012

提问by user3496755

to all my senior. I'm doing an assignment about Music Sales System.

给我所有的前辈。我正在做一个关于音乐销售系统的作业。

When the system begin will start a form "Login.vb", it allow the user to login with Staff ID and Password. After that, if login successful it will show the name of the staff in a textbox.

当系统开始时会启动一个表格“Login.vb”,它允许用户使用员工 ID 和密码登录。之后,如果登录成功,它将在文本框中显示员工姓名。

Could any senior help me or teach me how to retrieve the name when the staff login to the system?

有哪位前辈能帮我或者教教我员工登录系统时如何找回姓名吗?

Furthermore, I also like to retrieve picture from Access Database, hope can get some help from here.

此外,我也喜欢从 Access 数据库中检索图片,希望能从这里得到一些帮助。

I'm using Jet.Oledb.4.0.

我正在使用 Jet.Oledb.4.0。

Thank You.

谢谢你。

Below is my Login.vb code

下面是我的 Login.vb 代码

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

Private Sub btnLogin_Click(sender As Object, e As EventArgs) 处理 btnLogin.Click

    Dim Connection As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Music_Sales_Database.mdb;")
    Dim Command As New Data.OleDb.OleDbCommand("SELECT [Staff_ID] FROM [Staff] WHERE [Staff_IDField] = Staff_ID AND [PasswordField] = Password", Connection)

    Dim StaffIDParam As New Data.OleDb.OleDbParameter("Staff_ID", Me.txtStaff_ID.Text)
    Dim PasswordParam As New Data.OleDb.OleDbParameter("Password", Me.txtPassword.Text)

    Command.Parameters.Add(StaffIDParam)
    Command.Parameters.Add(PasswordParam)

    Command.Connection.Open()

    Dim Reader As Data.OleDb.OleDbDataReader = Command.ExecuteReader()

    If txtStaff_ID.Text = "" Then
        MessageBox.Show("Staff ID and Password cannot be empty, please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    ElseIf txtPassword.Text = "" Then
        MessageBox.Show("Staff ID and Password cannot be empty, please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    ElseIf Reader.HasRows Then
        MessageBox.Show("You are authenticated.", "Login Successful", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        MyPlayer.SoundLocation = path & LogOnSound
        MyPlayer.Play()
        txtPassword.Text = ""
        Me.Hide()
        Main_System.Show()

    Else
        MessageBox.Show("Invalid Staff ID or Password, please try again.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtPassword.Text = ""
        txtPassword.Focus()
    End If

    Command.Connection.Close()
End Sub

******BELOW is the program that Im coding, but still coding.......coz Im still new in VB.....hope can get some help

** ****BELOW 是我正在编码的程序,但仍在编码......因为我还是 VB 新手......希望能得到一些帮助

Dim Connection As New OleDb.OleDbConnection
Dim Command As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader
Dim DBPath As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Music_Sales_Database.mdb;"
Dim Image() As Byte

Private Sub RetrieveStaffInformation()
Connection.ConnectionString = DBPath
Connection.Open()

Command = Connection.CreateCommand
Command.CommandText = "SELECT        Staff.* FROM            Staff"

Reader = Command.ExecuteReader

Image = CType(Reader(0), Byte())
Dim MS As New System.IO.MemoryStream(Image)
Dim bmImage As New Bitmap(MS)

picStaff.Image = bmImage
picStaff.Refresh()
End Sub

回答by King of kings

I give you code for sql.change it to access

我给你 sql.change 的代码来访问

    Dim com As SqlCommand
    com = New SqlCommand("select uname,pwd from logg where uname='" & TextBox1.Text & "' and pwd='" & TextBox2.Text & "'", conn)
    Dim da As New SqlDataAdapter(com)
    Dim ds As New Data.DataSet
    da.Fill(ds)
    If ds.Tables(0).Rows.Count = 1 Then
        MsgBox("Successfully logged in")
        Label1.Text = ds.Tables(0).Rows(0)("uname")
    End If