I have table named Products and Categories, one of Products table's row include data like that:
ProductId,ProductName,CategoryId
And Categories include
CategoryId,CategoryInfo
So I m getting rows with using ExecudeReader() using ado.net like
List<ProductEntity> lst = new List<ProductEntity>();
using (SqlConnection connection = new SqlConnection(constr))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select ProductId,ProductName,CategoryId from Products";
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
lst.Add(new ProductEntity()
{
ProductId = int.Parse(reader[0].ToString()),
ProductName= reader[1].ToString(),
CategoryId=int.Parse(reader[2].ToString())
});
}
}
}
But I want to get CategoryInfo from Categories table too without using another ExecuteReader(). Can I create one entity class for both Product and Category if yes How I do that? Or another best practice exist?
cmdTexthas when used as command text?