none
How to fill objects RRS feed

  • Question

  • I have a table or tables in sqlServer. I want to take data from database and keep this table's data into objects like class, but I dont want to keep this data into dataset. What are those ways to keep data into objects taking data from database? How can I pass data into object but not in dataset or datatable?
    Wednesday, January 2, 2013 5:10 AM

Answers

  • You can go for Entity Framework.

    However, you can also do something like this,

    class Employee
    {
       int EmployeeId { get; set; }
       string Name {g et; set; }
       int DeptId { get; set;}
    }
    
    class DataMaster
    {
       public Employee GetEmployeeById(int empId)
       {
           Employee e = null;
           using(SqlConnetction conn = GetConnection())
           {
               using(SqlCommand command = conn.CreateCommand())
               {
                   ...
                   var reader = Command.ExecuteReader();
                   e = new Employee();
                   e.EmployeeId = reader["EmpId"];
                   e.Name = reader["Name"];
                   ...
               }
           }
           return e;
       }
    }

    I hope this helps.

     


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, January 2, 2013 6:11 AM
  • Hi,
    You can use Entity Framework or somewhat older version that is LINQ to SQL for your need.
    Using these techniques you do not need to write raw SQL for data manipulation or any other DB related task from UI application.
    And, as you wanted you do not have to use DataTables or DataSets.
    For more information you can visit :
    http://msdn.microsoft.com/en-us/data/ef.aspx
    http://msdn.microsoft.com/en-us/library/bb386976.aspx

    One good question is equivalent to ten best answers.

    Wednesday, January 2, 2013 8:13 AM

All replies

  • You can go for Entity Framework.

    However, you can also do something like this,

    class Employee
    {
       int EmployeeId { get; set; }
       string Name {g et; set; }
       int DeptId { get; set;}
    }
    
    class DataMaster
    {
       public Employee GetEmployeeById(int empId)
       {
           Employee e = null;
           using(SqlConnetction conn = GetConnection())
           {
               using(SqlCommand command = conn.CreateCommand())
               {
                   ...
                   var reader = Command.ExecuteReader();
                   e = new Employee();
                   e.EmployeeId = reader["EmpId"];
                   e.Name = reader["Name"];
                   ...
               }
           }
           return e;
       }
    }

    I hope this helps.

     


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, January 2, 2013 6:11 AM
  • Hi,
    You can use Entity Framework or somewhat older version that is LINQ to SQL for your need.
    Using these techniques you do not need to write raw SQL for data manipulation or any other DB related task from UI application.
    And, as you wanted you do not have to use DataTables or DataSets.
    For more information you can visit :
    http://msdn.microsoft.com/en-us/data/ef.aspx
    http://msdn.microsoft.com/en-us/library/bb386976.aspx

    One good question is equivalent to ten best answers.

    Wednesday, January 2, 2013 8:13 AM