wftico
wftico

Reputation: 73

C# - retrieve data from a DB and assign it to a variable

I have a very basic task I want to do but have no idea as how to approach it. The issue:

What I did
Created a Windows Forms Project. Created a database (Microsoft MySQL Database) and created a table there. The database itself works so far.

What I want
I want to take a specific integer from the database that belongs to a certain ID (key) and store that integer value to a variable in my C# Windows Forms program.

Example: look at the 'ID 60' in the database and the value of 'grade' that belongs to that ID. Now let the text of a label (let's say label1.Text = ) get that value.

How do I do it?
It's a very simple basic application. I don't have to bother with any safety issues whatsoever.

Big Picture
The ID's are the points that can be achieved from an exam. That would be 1-60. Each ID has 6 grades. Each grad has a max and min value (a range, from which points that grade is supposed to be).

Basically I'd also want to do calculations later on with the DB value. For example look at ID 60 and grade B max value, if the user input is equal to the grade B max value then the grad will be B+.

For that I need to be able to efficiently retrieve values from the DB and I currently don't know how to do it. With SELECT commands, or are there better/other ways with C#?

Thank you a lot!

[This is not an assignment or something like that, I just want to help my GF out who's a teacher and could use a little program like that] :)

Upvotes: 1

Views: 565

Answers (1)

Attersson
Attersson

Reputation: 4866

What you are looking for, as a step up from raw querying, is Object-relational mapping (ORM)

In order not to reinvent the wheel, I recommend this answer Best ORM to use with C# 4.0

This answers your question. As for what option suits best, that is a matter of opinion, not to be discussed here.

Upvotes: 2

Related Questions