Sounds like what you are after is using
T4[
^] (Text Template Transformation Toolkit) to generate your code. Basically, you will start with a template of the code you need for your functionality, then you'll open up a connection to the database to read the table structures in - there are different ways of doing this depending on the database you are using, and finally you will apply this information to your template. Start with the page I linked to and then work outwards from there.