Getting Visual Basic to read from a database....

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi All!
Thanks for taking the time to read my question.
I am trying to write a VB program that will allow me to read the value of Device Name from a table called DeviceDetails in an Access database (.mdb). Ive included the script as it stands at the moment, which compiles and runs but Im trying to
get over the stumbling block whereby when I choose a database file the result written to my Textbox2 is "System.Data.OleDb.OleDbDataReader" which I think means Ive just turned my.Reader into a string...?

Ive never used VB before so any help would be greatly appreciated!
<pre class="prettyprint lang-vb Imports System.Data.OleDb

Public Class Form1

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
strm.Close()
MessageBox.Show("File Selected")
End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = ""
OpenFileDialog1.Title = "Please Select File"
OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.Filter = "Database File (*.mdb) |*.mdb"
OpenFileDialog1.ShowDialog()
Dim filename As String = "ofd.Filename"
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + OpenFileDialog1.FileName)
Dim myCommand As New OleDbCommand("select device_name from DeviceDetails")
myCommand.Connection = myConnection
myConnection.Open()
Dim myReader As OleDbDataReader
myReader = myCommand.ExecuteReader()
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
TextBox2.Text = myReader.ToString
myReader.Close()
myConnection.Close()
End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub

Public Sub ReadDatabase(myConnectionString As String)
If myConnectionString = "" Then
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0"
End If

End Sub


End Class[/code]
<br/>



View the full article
 
Back
Top