Jump to content

C# (visual studio) fill a table with results from database

YouRezz

hey

so as a part of my end-od-year project i have to create a simple game in c# (i picked a matching game)  and i also have to display score at the end. This is the part where i have litterally no idea how to do it.

i have attached my form as an image, every cell inside that table has a label. i have a .mdb database connected wich contains only one table with attributes : user, pass and score.

i need to write the username in the "ime" column and score in the score column 

 

dont get distracted by the buttons at the bottom, as they are just replay the game and close the programme. 

 

thanks for help :D

post-15684-0-31067400-1433870291.png

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

What have you tried so far? Do you have any existing code to show? What do you struggle with?

 

It's been a while since I've done any DB access in C# but the basic concept is something like this : fetch results from database -> iterate over results -> for every iteration, add the new row to the table. Which part of that are you having trouble with?

Link to comment
Share on other sites

Link to post
Share on other sites

Usually you can simply plug all of this together in the visual forms designer as if playing with Lego if I recall correctly.

 

You can use a DataGridView which gives you a drop down for it's data source from where you may select a BindingSource. From this you set it's DataSource property; doing this in the designer you are guided by wizards most of the way. You will be offered a dialogue where you can select the database (your file) and it should give you a DataSet component and plug it in for you at the end. You can even use the BindingNavigator to add pagination if required - again you just need to plug them together in the designer.

 

I say the designer because it's often preferable to writing all of this out and wiring it all up manually yourself in code. Yes that is also a correct approach (and helps one understand how it all works) but for this, when you likely don't really need any custom intervention? why reinvent the wheel?

 

Be mindful of data driver compatibility if running under x64 or on some of the newer operating systems i.e. Windows 8.1 and Windows 7 in particular. You may need to install tools for office/access and maybe the data drivers themselves.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

thanks, im gonna try to do this as you said, i know how to connect to a database and read files from it, i just dont have an idea of how to acutally get the data i got into the labels in the table. 

      connection.ConnectionString = @"PROVIDER=Microsoft.Jet.OLEDB.4.0;                                                Data Source=C:\Users\Jure\Documents\uporabniki.mdb";            connection.Open();            OleDbCommand command = new OleDbCommand();            command.Connection = connection;            command.CommandText = "select username,highscore from uporabniki where username = '"+ user  +"' ";            OleDbDataReader reader = command.ExecuteReader();                                                   while (reader.Read())                {                    string tvojscore = reader["score"].ToString();                }            

i dont know what to put into the while loop :D

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

thanks, im gonna try to do this as you said, i know how to connect to a database and read files from it, i just dont have an idea of how to acutally get the data i got into the labels in the table. 

 

Unless you have been specifically instructed not the change it then I wouldn't use that static structure at all for displaying the data. Far better to use the DataGridView as I suggested. It should populate according to the shape of the data you feed into it. Moreover you should still be able to customize how it is presented/displayed if needed.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×