Associating a database with objects

I do not know if this is even the correct terminology, but I was wondering if it is possible to associate a row of (data) from the database with the object. I am working on a school project, and we decided to create this application to track the user's BMI / BMR and compare it with the number of calories entered. The program does not go too deep, so it does not store the calories consumed in the database.

So here is my problem. I have a row in my database:

First_Name - Last_Name - Height_Inches - Weight_Pounds

And I suppose I think I want my program to execute - if I have an object with properties for FirstName, LastName, HeightInches, WeightPounds - it somehow query the database for one row and put the values ​​in the object . Everything that we have learned so far associates data with controls, which actually do not help in my case, since the work will be done behind the scenes.

Sorry if this is vague.

0
source share
1 answer

I'm not very good at VB.Net syntax, so here are some C # that might help :) It should be easy enough to translate. Also, some time has passed since I made raw ADO.Net, so it may not be entirely accurate.

  using (SqlConnection conn = new SqlConnection("ConnectionStringGoesHere")) { string query = @"select First_Name, Last_Name, Height_Inches, Weight_Pounds from table_name"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); IDataReader reader = cmd.ExecuteReader(); BMIObject bmi = new BMIObject(); bmi.FirstName = (string)reader["First_Name"]; bmi.LastName = (string)reader["Last_Name"]; bmi.Weight = (int)reader["Weight_Pounds"]; bmi.Height = (int)reader["Height_Inches"]; reader.Close(); return bmi; } 
0
source

Source: https://habr.com/ru/post/1399463/


All Articles