ADO.net dataset example

Ado.net dataset to list example

A dataset is a in memory copy of your database. In consist of an amount of datatables including the relation to one another and commands to write the updates back to the database.


public class Employee
    {
        public string Voornaam { get; set; }
        public string Achternaam { get; set; }
    }

    public class Employees
    {
        private const string connectionString = @"Server=serverName;Database=databaseName;User Id=userName;Password=passWord;";

        public static List GetEmployees()
        {
            // create list, dataset and datatable
            List employeelist = new List();
            DataSet employeeData = new DataSet("EmployeeData");
            DataTable employeeTable = new DataTable("Employees");
            employeeData.Tables.Add(employeeTable);

            // execute query
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand customerQuery = new SqlCommand("select * from employees", sqlConnection))
                {
                    using (SqlDataAdapter customerAdapter = new SqlDataAdapter(customerQuery))
                    {
                        try
                        {
                            customerAdapter.Fill(employeeData, "Employees");
                        }
                        finally
                        {
                            if (sqlConnection.State != System.Data.ConnectionState.Closed)
                            {
                                sqlConnection.Close();
                            }
                        }
                    }
                }

                // create list from datatable
                employeelist = employeeData.Tables["Employees"].AsEnumerable().Select(row => new Employee
                {
                    Voornaam = row.Field(0),
                    Achternaam = row.Field(1)
                }).ToList();

                return employeelist;
            }
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!