Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#3.5
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?
Posted 1-Jan-13 19:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is one way.. create entity class having property same like your table columns,
then retrieve data from database in SqlDataReader, then traverse through the SqlDataReader, create an object of entity class assingn the value of each column to rispective property of your object and add the object in List.
 
eg // assume my table contains only two fields Name and Address
Public Class Student
{
  Public string Name
 {
  get;
  set;
 }
 Public string Address
 {
  get;
  set;
 }
 
 Public List<student> GetAllData()
 {
  List<student> lstStudent = new List<student>();
  //here code to fetch data from database
  While(reader.Read())
  {
     Student obj = new Student();
     obj.Name = reader["Name"].ToString();
     Obj.Address = reader["Address"].ToString();
     lstStudent.Add(obj);
  }
  return lstStudent;
 }
}
</student></student></student>
If the solution is helpful to you the accept the solution
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

public class exampleObject
{
public string Column1FromTablex {get; set;}
public string Column2FromTabley {get; set;}
...
}
 
//Then you can use SQLCommand to execute query against database
//Then you can iterate through the results and build the objects
//Ex: "Select column1 From Example1.dbo.Tablex..." and use the query to get exactly the columns you want for your "exampleObject"
  Permalink  

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

  Print Answers RSS
0 DamithSL 270
1 OriginalGriff 184
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Sergey Alexandrovich Kryukov 125
0 OriginalGriff 7,760
1 DamithSL 5,874
2 Sergey Alexandrovich Kryukov 5,444
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100