1

I want to map data from datatable to custom .NET object. Let's say, I have a .NET object

Public Class MyClass
    Public ID
    Public Prop1
    Public Prop2
End Class

And a corresponding table in the database, which contains columns ID, Prop1 and Prop2.

I want to generate a list of .NET objects from the database. Currently I fill the dataset and map each property individually. Is there any way, to map the database automatically, so the properties would map according to property / column name.

'retreive dataset from db'
DaAdapter.Fill(ds)

'create a new list of my object'
Private l As New List(Of MyClass)

'iterate through dataset (this is the step I want to get rid of - of course'
'it could only work if the property of the MyClass and the column in '
'the database have the same name)'
For Each r As DataRow in ds.Tables(0).Rows
    Dim itm As New MyClass
    itm.ID = r("ID")
    itm.Prop1 = r("Prop1")
    itm.Prop2 = r("Prop2")
    l.Add(itm)
Next

4 Answers 4

1

You can use some sort of Object Relational Mapping (ORM) framework. Entity Framework is one of them

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

Comments

1

IMHO. The easiest way is using Linq2Sql

Comments

1

There is no direct way , although many tools are available to create classes that exactly resembles database tables called Entity classes. See the following link for more details

Database tables to C# entity classes - generator for FluentNHibernate?

You can also use typeddataset, codesmith tool.

Comments

0

I suggest Entity Framework

You can use a database first approach where you just point to your database and it will create the Entities and associations for you.

Its really strait forward, I advise giving it a try

Comments

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.