2014-04-04 63 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 
相關問題