Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
please give me Data Mapping example from sql database in C#
Posted

1 solution

Entity Framework is a data mapper example. Using a framework like this saves you a lot of time and many headaches.

Just to show you the kind of fun you're in for without a mapping framework, this is a simple read-only data mapping example (without using entity framework):

C#
public class MyTableEntryObject
{
    public int Id { get; set; }
    public string Name { get; set; }
...
}

var thisIsMapped = new List<MyTableEntryObject>();
using(var conn = new SqlConnection("MyServerName"))
{
    using(var command = new SqlCommand("SELECT * FROM tbl.myTable",conn))
    {
        using(var reader = SqlCommand.ExecuteReader())
        {
            if(read.HasRows)
            {
                while(reader.Read())
                {
                    var mappedObject = new MyTableEntryObject();
                    mappedObject.Id= reader.GetInt32(0);
                    mappedObject.Name = reader.GetString(1);
                    // other fields
                    ...
                }
            }
        }
    }
}    
</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900