Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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"
 
Share this answer
 
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
 
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