In this tutorial i’ll show you how you can quick and easy access a dBase File and read it’s content.
Fire up your Microsoft Visual Studio 2010 , if you don’t have already Visual Studio you can download the express Version for free on the Microsoft Page.
Select “New Project” and create a new Windows-Forms Application, in the lower left corner you can enter your project name
Now select “View” and “Designer” you should now see a blank window.
Make sure you have the Toolbox in the “View” enabled. Now create a “Richtextbox” and a simple “Button” via Drag’n'Drop from the Toolbox to the empty Window in the designer.
Now Double-Click on the button to get to the Button clicked Event. You’re now able to view the code Window, it should look something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub
Now everything that is written inside this two lines will be executed when the button is clicked.
So here’s the example code for connecting to a dbase IV database:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Let's get ready Dim ConnectionString As String ' A connection String consists of a Provider in this case we use the OLEDB and a DATA Source '(where you're file is located) it can contain also user login information and everything else required 'when connecting to a database. You can get every connection string by searching it via google. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:dBase;Extended Properties=dBase IV" ' My File is located in c:dBase Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(ConnectionString) 'Create a new Connection Object dBaseConnection.Open() ' Open the connection ' Important! The File Name is given in the select Command,e.g. MyDBase Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("SELECT * FROM MyDBase", dBaseConnection) ' This is a typical simple SQL Statement, you can write a lot of other types of sql statements here 'but for now this will do. It means select everything from the tableMyDbase. Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess) 'Set the Access to sequential access While dBaseDataReader.Read ' do this as long as there is data to be read 'The Text of the Richtextbox1 is set to it's text + the dBaseDataReader' ., the 4 in brackets means that the 4th row is written. the .toString command converts the data to a string RichTextBox1.Text = RichTextBox1.Text + " " + dBaseDataReader(4).ToString() End While 'the end of the loop dBaseConnection.Close() ' That's it, now close the connection End SubEnd Class
Now you can hit the play button to compile your application.
If you hit the Button, the Richtextbox will be filled with the Values from your dBase Database.