// Get a set of objects from the data provider public static List < Customers > GetCustomers () { return CustomersData .GetAllCustomers (); } }
and you need to get a set of Customers from the vendor .
Standard solution to this issue in. NET 2.0 is the use of the System namespace . Data . SqlClient :
public static class CustomersData { // Use SqlClient public static list < Customers > GetAllCustomers () { List < Customers > lst = new List < Customers > (); using ( SqlConnection conn = new SqlConnection ( "Data Source = ..." )) { conn.Open (); SqlCommand cmd = new SqlCommand ( "SELECT CustID, Name, Age FROM tbCustomers" , conn); SqlDataReader reader = cmd.ExecuteReader (); while (reader.Read ()) { lst.Add ( new Customers (( Int64 ) reader [ "CustID" ], ( String ) reader [ "Name" ], ( Int32 )? reader [ "Age" ])); } } return lst; } }
When using Linq, we can get a set of data from anonymous types using the object's initializer, i.e.
public static class CustomersData { // Using Linq public static list < Customers > GetAllCustomers () { LinqDcDataContext db = new LinqDcDataContext (); IEnumerable < Customers > result = db.tbCustomers.Select (c => new Customers (c.CustID, c.Name, c.Age)); // Set IEnumerable <Customers> to List <Customers> return new List < Customers > (result); } }