2

How do you create a new class object and add data to it from a database?

I have a table(tblCars) in a database and would like to loop through each row(Using sqldatareader?) and create a new object named after the primary key(Registration number), then add data to it from the other columns.

So instead of this:

carClass car1 = new carClass();
car1.registration = "RT11 HWY";
car1.make = "Ford";

It would be something like:

//connection and reader stuff
carClass <registration> = new carClass();
<registration>.registration = column1;
<registration>.make = column2;

1 Answer 1

4

You can use some lightweight ORM for this kind of mapping. Take a look here, there are many free solutions for this. There are also at least two made by Microsoft, LINQ to SQL and Entity Framework

Sign up to request clarification or add additional context in comments.

1 Comment

Also: see Dapper Dot Net which powers this site :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.