Last active
May 25, 2016 17:37
-
-
Save ChrisMoney/3d4ed2d215d40f255626b70aebe718f2 to your computer and use it in GitHub Desktop.
Entity Framework using Collection or ADO.NET Datatable (Code First) Without MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product { | |
private string modelNumber; | |
public string ModelNumber { | |
get { return modelNumber; } | |
set { modelNumber = value; } | |
} | |
private string modelName; | |
public string ModelNumber { | |
get { return modelName; } | |
set { modelNumber = value; } | |
} | |
private decimal unitCost; | |
public decimal UnitCost { | |
get { return unitCost; } | |
set { unitCost = value; } | |
} | |
private string description; | |
public string Description { | |
get { return description; } | |
set { description = value; } | |
} | |
} | |
// get data from database and attach to Product class | |
public List<Product> GetProducts() { | |
SqlConnection con new SqlConnectiion(connectionString); | |
SqlCommand cmd = new SqlCommand("GetProducts", con); | |
cmd.CommandType = CommandType.StoredProcedure; | |
List<Product> products = new List<Product>(); | |
try { | |
con.Open(); | |
SqlDataReader reader = cmd.ExecuteReader(); | |
while (reader.Read()) { | |
// create a Product object that wraps the current record | |
Product product = new Product((string)reader["ModelNumber"], (string)reader["ModelName"], (decimal)reader["UnitCost"], | |
(string)reader["Description"], (string)reader["CategoryName"], (string)reader['ProductImage']); | |
} | |
} | |
// Add to Collection | |
products.Add(product); | |
} | |
} | |
finally | |
{ | |
con.Close(); | |
} | |
return products; | |
} | |
//------------------------------------------------------- | |
//Load Entity Object Using ADO.NET | |
Public DataTable GetProducts() { | |
SqlConnection con new SqlConnectiion(connectionString); | |
SqlCommand cmd = new SqlCommand("GetProducts", con); | |
cmd.CommandType = CommandType.StoredProcedure; | |
DataTable dt = new DataTable(); | |
adapater.Fill(dt); | |
return dt; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment