Sunday, 31 May 2015

c# database application part 1

Firstly this will be a short post just explaining how to create a connection to a database from a c# application and how to make a simple select statement run and show results.

Tools:

Microsoft Access 2013 (MA)
Visual studio 2010 (VS)

Firstly open VS and create a new windows application.
Go into the code view and add the following to the list of using statements:

using System.Data.OleDb;

The next part is to create a oledb connection to allow us to connect to the database, I have created mine as to make it a global connection allowing anything on the form to access it. The code for this looks like this:

OleDbConnection connection = new OleDbConnection();

And it is placed at the beginning of the program, like so:


I have also added the code that is on line 20,  this is the connection string and is supplied with the path file name of where your database is located on your machine.

connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Insert path file here;Persist Security Info=False;";

Changing the bold text to your file path will allow your program to find your database and connect to it. The connection string may differ on different versions of software as well as between different software, to check use this website:

http://www.connectionstrings.com/

Next create a form load event by going to the GUI part of the form and double clicking somewhere on the form, you will be returned to the code view but with an event created; this will run whenever the form is loaded.

Add the following try catch statement to your newly created event:


This try catch statement will attempt to connect to your chosen database and if it cannot it will display the error preventing it in a message box. For example if I incorrectly type the file path for the connection string this error will appear:


Line 27 opens the connection that was established at the beginning of the program.
Line 28 changes the text for a label I have placed on the form to display the connection status, it is not needed for the program to work but I found it useful whilst I'm working on the program. It is also worth noting that line 27 and line 39 are the only ones of the try statement required to open and close a connection to the database and that everything else in between is there for the SELECT statement.
Line 29 creates the command object used to execute the SELECT statement.
Line 30 assigns the connection string to the command object so it knows which connection to execute on.
Line 31 creates the string query that will be executed. Mine is just a simple Select statement that gets everything from a table called 'game' in my database,
Line 32 assigns the query to the command so it knows what it is executing.
Line 34 creates a data table adapter object and passes the command as its parameter.
Line 35 creates a data table object.
Line 36 fills the data table with the results of the command that was passed to the adapter. So currently it fills it with all the data found in the 'game' table.
Line 37 assigns the data table object to a data grid view object on the form called DGV_databaseWindow.
Line 39 closes the connection to the database. I've read its good practice to close any connection after your done but I'm not sure if it actually prevents errors or just an extension of some programming structure guidelines.
Line 41- 44 is just the catch statement that was explained earlier and displays an error if one is encountered.

Next add a data grid view control to the GUI part of your form (found in toolbox) and change its name to DGV_databaseWindow,
As long as you've created your database and its where the file path suggests it is everything should run fine and you should get a screen like this:


So if anyone besides me is reading this and has any questions, feel free to contact me and i'll try and answer the best I can.

FF10 - Guadosalam