|
-
Nov 14th, 2008, 09:07 AM
#1
Thread Starter
Lively Member
No results from sql query
Hi.
Got a small problem here. Trying to read data from an Access 2007 database but I'm getting nowhere.
What I do know is that everything is spelled correctly and that the database actually contains data.
I get no error and the form launches but the labeltext is not changed.
thanks
btw, Using VS 2008
Code:
Imports System.Data.OleDb
Public Class Form1
Private conStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SoccerQuiz.accdb;Jet OLEDB:Database Password=I_hate_Inter"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReadData(conStr, "SELECT * FROM Tabell")
End Sub
Public Sub ReadData(ByVal connectionString As String, _
ByVal queryString As String)
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
lblTest.Text = reader(0).ToString()
'Console.WriteLine(reader(0).ToString())
End While
reader.Close()
End Using
End Sub
End Class
this code doesn't work either:
Code:
conn = New OleDbConnection(connstring)
conn.Open()
Dim cmd As New OleDbCommand(sql_query, conn)
Try
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While reader.Read()
MessageBox.Show(reader.GetValue(0).ToString())
End While
reader.Close()
Catch err As OleDbException
MessageBox.Show(err.Message)
Finally
conn.Close()
End Try
FORZA ROSSONERI! CAMPIONI!!!
-
Nov 14th, 2008, 12:05 PM
#2
Re: No results from sql query
Try this
Code:
Imports System.Data.OleDb
Public Class Form1
Private connstring As String = "Your connection string"
Private conn As OleDb.OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection(connstring)
conn.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSQL As String = String.Empty
sSQL = "SELECT * FROM Tabell "
Dim command As New OleDbCommand(sSQL, conn)
Dim reader As OleDbDataReader = command.ExecuteReader()
ListBox1.Items.Clear()
If reader.HasRows = True Then 'make sure there is data returned
While reader.Read()
ListBox1.Items.Add(reader.GetString(0))
End While
Else
MessageBox.Show("No data matched your search criteria.")
End If
reader.Close()
End Sub
End Class
You are returned multiple records so I would suggest using something other than a label.
-
Nov 14th, 2008, 07:43 PM
#3
Thread Starter
Lively Member
Re: No results from sql query
Thanks Hack.
While this did not solve my problem I now get errors that I can try to resolve. For example "Microsoft.ACE.OLEDB.12.0-provider is not registerd on the computer". (Wich I guess is a big reason why it wont work ). I will try to figure this out in the morning. If someone knows how to fix this so that I don't need to spend an hour on Google I would be a very hapy dude 
Thanks again.
Last edited by Rossonero; Nov 14th, 2008 at 07:52 PM.
FORZA ROSSONERI! CAMPIONI!!!
-
Nov 15th, 2008, 07:02 AM
#4
Re: No results from sql query
 Originally Posted by Rossonero
Thanks Hack.
While this did not solve my problem I now get errors that I can try to resolve. For example "Microsoft.ACE.OLEDB.12.0-provider is not registerd on the computer". (Wich I guess is a big reason why it wont work  ). I will try to figure this out in the morning. If someone knows how to fix this so that I don't need to spend an hour on Google I would be a very hapy dude 
Thanks again.
That means that the system it's being run on doesn't have any Office drivers installed. To get the Access 2007 OLEDB provider you have to either install Access 2007 or the Office driver pack.
http://www.microsoft.com/downloads/d...displaylang=en
With regards to your original issue, you were using a While loop and testing the result of the DataReader's Read method. Most likely the method never returned True so the loop was never entered. That means that the query returned no data. You can confirm that by testing the reader's HasRows property.
-
Nov 15th, 2008, 03:19 PM
#5
Thread Starter
Lively Member
Re: No results from sql query
FORZA ROSSONERI! CAMPIONI!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|