2014-04-04 38 views
0

给我所有的大四学生。我正在做一个关于音乐销售系统的任务。如何在用户登录到程序时从Access数据库检索数据?

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

任何老人都可以帮助我,或教我如何在员工登录系统时检索姓名?

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

我正在使用Jet.Oledb.4.0。

谢谢。

下面是我Login.vb代码

私人小组btnLogin_Click(发送者为对象,E作为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是Im编码的程序,但仍然编码....... coz我仍然在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 
+0

告诉我们您的代码,请相关部分.. – Codemunkeee

+0

@Codemunkeee我发布的代码... THX – user3496755

+0

的login.vb为100%工作,但我不知道如何编码从访问检索数据时,用户登录成功 – user3496755

回答

0

我给你的代码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 
相关问题