How can I show my data table as a list in a console application?
I have a local Database called = MyDatabase. And I have a table called = tbl_cars.
How can I convert tbl_cars to a list?
How can I show my data table as a list in a console application?
I have a local Database called = MyDatabase. And I have a table called = tbl_cars.
How can I convert tbl_cars to a list?
Fill DataTable from your database and Create a custom class containing all the fields of tbl_cars table.
DataTable dt = FetchTableFromDatabase();
List<Car> lst = new List<Car>();
lst = (from DataRow row in dt.Rows
select new Car
{
ID = (int)row["ID"],
CarName = row["CarName"].ToString()
}).ToList();
private DataTable FetchTableFromDatabase()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Your connectionString");
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_cars", con);
try
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}