mardi 27 janvier 2015

Not able to read data from access database

I have a vb form in visual studio 2010 and I want to put data from it by importing the data from an access database. What I'm trying to create is a multiple answer quiz for my students. In my database I have 30 questions but I only want to randomly select 10. This is what my database look like. Database view


This the the code that I have for this part.



Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader


Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dataFile = "Data Source =F:\Quiz\Programs\UNZipped\questions.accdb; User Id =admin; Password=password;"
connString = provider & dataFile
myConnection.ConnectionString = connString

myConnection.Open()

Dim str As String
str = "SELECT Top 10 ID_Question From Questions ORDER BY RND(ID_Question)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader


While dr.Read()

Label3.Text = dr("ID_Question").ToString
MsgBox("test")
End While
myConnection.Close()
MsgBox("fsafa")
End Sub


The code run with no errors but it seems like the connection does not work. I tried changing Microsoft.Jet.OLEDB.4.0 by Microsoft.Ace.OLEDB.12.0 like someone suggested on another similar question. I'm quite new at this kind of stuff so I'd like to get some help.


Thank you





Aucun commentaire:

Enregistrer un commentaire