'Convert database row into object
I have a table in my database with 2 rows of employee data.
...........
ID | Name
...........
1 | Jon Doe
2 | Jane Doe
...........
I need this in my code, just like this, as code. I need to convert this into:
Employee e1 = new Employee();
e1.ID = 1;
e1.Name = "Jon Doe";
Employee e2 = new Employee();
e2.ID = 2;
e2.Name = "Jane Doe";
Now it is code it can be saved into a database when the app runs for the first time, and thus save me from writing 1200 object instances into the database. Therefore, I just need some way, such as using XML) on the first run.
This is an example, why I need it is because, I have a table that stores settings (width,height,label for grid) etc that need to be set to something first time the app runs.
What way could I generate this, are there any tools available, or how would you go about it?
Solution 1:[1]
Edited I appear to have misread your question. It looked like what you want is an orm mapper but really what you are looking for is something like MyGeneration which you can point at a database and have it build you the objects you need. I leave the remainder of the original answer as an ORM is still useful in these situations.
There are several available for .Net and if you are developing on .Net 3 or higher, than you have one built in call Linq. Getting started with Linq is beyond the scope of this answer so here is a great blog post from Scott Guthrie which walks you through it. Another Microsoft offering is available called EntityFramwork and more information about that can be found here
NHibernate is a free .Net port of the Hibernate framework which also gets the job done.
ORM mappers make converting database data into classes very easy, but they come with trade offs. See the wikipedia entry for more info.
Solution 2:[2]
Why don't you select the data from db in this format and save it to a file?
For mysql I would do (and did):
Save this query as c:\in.sql
SELECT CONCAT('Employee e', id, ' = new Employee();', char(13),
'e', id, '.ID = ', id, ';', char(13),
'e', id, '.Name = "', name, '";',
char(13), char(13)) as '//generated' from employee;
and run from commandline:
mysql -u root < c:\in.sql > c:\out.cs
This will execute the query from in.sql and put the output to out.cs in this format:
//generated
Employee e1 = new Employee();
e2.ID = 1;
e2.Name = "...";
Employee e2 = new Employee();
e2.ID = 2;
e2.Name = "...";
Solution 3:[3]
You can use something like Entity Framework, Linq2SQL, SubSonic, nHibernate, etc.. to generate objects based on your database schema.
Solution 4:[4]
There are many ORM tools (as Cody C suggested) that will generate code and do the mapping for you. But from the sound of the question, I am thinking that would be overkill for your application. If this is all you need, run your query with your perfered data access (dynamic sql, stored proc, views, etc) and then simply map the values in the returning data to your object.
This is certainly not the most elegant solution, but introducing a 3rd party tool may be more than you need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jürgen Steinblock |
| Solution 3 | Cody C |
| Solution 4 | Mike Ohlsen |
