示例#1
0
        private static void Stap6c()
        {
            Console.WriteLine("Gegevens ophalen...");
            string query = "SELECT Number, Name, Teacher, CourseType, StartDate, "
                           + "LaptopRequired, MaximumParticipants, Price FROM Courses "
                           + "WHERE LaptopRequired = @p_required";
            List <Models.Course> courses = new List <Models.Course>();

            using (SqlConnection conn = GetConnection())
            {
                //Toewijzen van zowel query alsook de connectie aan het commando object
                //Ook een parameter gaan we toevoegen aan het commando. De naam is dezelfde
                //als deze in de query, de waarde geven we als tweede parameter mee
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@p_required", false);
                conn.Open();

                SqlDataReader reader       = cmd.ExecuteReader();
                int           ixNumber     = reader.GetOrdinal("Number");
                int           ixName       = reader.GetOrdinal("Name");
                int           ixTeacher    = reader.GetOrdinal("Teacher");
                int           ixCourseType = reader.GetOrdinal("CourseType");
                int           ixStartDate  = reader.GetOrdinal("StartDate");
                int           ixLaptopReq  = reader.GetOrdinal("LaptopRequired");
                int           ixMax        = reader.GetOrdinal("MaximumParticipants");
                int           ixPrice      = reader.GetOrdinal("Price");
                while (reader.Read())
                {
                    Models.Course loadingCourse = new Models.Course();
                    loadingCourse.CourseId = reader.GetInt32(ixNumber);

                    loadingCourse.Name = reader.GetString(ixName);
                    loadingCourse.Teacher
                        = reader.IsDBNull(ixTeacher) ? null : reader.GetString(ixTeacher);
                    loadingCourse.CourseType = reader.GetString(ixCourseType);
                    if (!reader.IsDBNull(ixStartDate))
                    {
                        loadingCourse.StartDate = reader.GetDateTime(ixStartDate);
                    }
                    loadingCourse.LaptopRequired      = reader.GetBoolean(ixLaptopReq);
                    loadingCourse.MaximumParticipants = reader.GetInt32(ixMax);
                    if (!reader.IsDBNull(ixPrice))
                    {
                        loadingCourse.Price = reader.GetDouble(ixPrice);
                    }
                    courses.Add(loadingCourse);
                }
                reader.Close();
                conn.Close();
            }
            foreach (var item in courses)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Gegevens ophalen gelukt"); Console.ReadLine();
        }
示例#2
0
        private static void Stap6a()
        {
            Console.WriteLine("Gegevens ophalen...");
            string query = "SELECT Number, Name, Teacher, CourseType, StartDate, "
                           + "LaptopRequired, MaximumParticipants, Price FROM Courses";
            List <Models.Course> courses = new List <Models.Course>();

            using (SqlConnection conn = GetConnection())
            {
                //Toewijzen van zowel query alsook de connectie aan het commando object
                SqlCommand cmd = new SqlCommand(query, conn);
                conn.Open();
                //Commando uitvoeren op de DB en de resultatentabel uitlezen via onze
                //DataReader cursor
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Models.Course loadingCourse = new Models.Course();
                    loadingCourse.CourseId   = reader.GetInt32(0);
                    loadingCourse.Name       = reader.GetString(1);
                    loadingCourse.Teacher    = reader.IsDBNull(2) ? null : reader.GetString(2);
                    loadingCourse.CourseType = reader.GetString(3);
                    if (!reader.IsDBNull(4))
                    {
                        loadingCourse.StartDate = reader.GetDateTime(4);
                    }
                    loadingCourse.LaptopRequired      = reader.GetBoolean(5);
                    loadingCourse.MaximumParticipants = reader.GetInt32(6);
                    if (!reader.IsDBNull(7))
                    {
                        loadingCourse.Price = reader.GetDouble(7);
                    }
                    courses.Add(loadingCourse);
                }
                //Cursor ook afsluiten
                reader.Close();
                conn.Close();
            }
            foreach (var item in courses)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Gegevens ophalen gelukt");
            Console.ReadLine();
        }
示例#3
0
        private static void Stap6b()
        {
            Console.WriteLine("Gegevens ophalen...");
              string query = "SELECT Number, Name, Teacher, CourseType, StartDate, "
              + "LaptopRequired, MaximumParticipants, Price FROM Courses "
              + "WHERE LaptopRequired = @p_required";
              List<Models.Course> courses = new List<Models.Course>();
              using (SqlConnection conn = GetConnection())
              {
            //Toewijzen van zowel query alsook de connectie aan het commando object
            //Ook een parameter gaan we toevoegen aan het commando. De naam is dezelfde
            //als deze in de query, de waarde geven we als tweede parameter mee
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@p_required", false);
            conn.Open();

            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
              Models.Course loadingCourse = new Models.Course();
              loadingCourse.CourseId = reader.GetInt32(0);
              loadingCourse.Name = reader.GetString(1);
              loadingCourse.Teacher = reader.IsDBNull(2) ? null : reader.GetString(2);
              loadingCourse.CourseType = reader.GetString(3);
              if (!reader.IsDBNull(4))
            loadingCourse.StartDate = reader.GetDateTime(4);

              loadingCourse.LaptopRequired = reader.GetBoolean(5);
              loadingCourse.MaximumParticipants = reader.GetInt32(6);
              if (!reader.IsDBNull(7))
            loadingCourse.Price = reader.GetDouble(7);
              courses.Add(loadingCourse);
            }
            //Cursor ook afsluiten
            reader.Close();
            conn.Close();
              }
              foreach (var item in courses)
            Console.WriteLine(item);
              Console.WriteLine("Gegevens ophalen gelukt");
              Console.ReadLine();
        }
示例#4
0
 private static void Stap6a()
 {
     Console.WriteLine("Gegevens ophalen...");
       string query = "SELECT Number, Name, Teacher, CourseType, StartDate, "
       + "LaptopRequired, MaximumParticipants, Price FROM Courses";
       List<Models.Course> courses = new List<Models.Course>();
       using (SqlConnection conn = GetConnection())
       {
     //Toewijzen van zowel query alsook de connectie aan het commando object
     SqlCommand cmd = new SqlCommand(query, conn);
     conn.Open();
     //Commando uitvoeren op de DB en de resultatentabel uitlezen via onze
     //DataReader cursor
     SqlDataReader reader = cmd.ExecuteReader();
     while (reader.Read())
     {
       Models.Course loadingCourse = new Models.Course();
       loadingCourse.CourseId = reader.GetInt32(0);
       loadingCourse.Name = reader.GetString(1);
       loadingCourse.Teacher = reader.IsDBNull(2) ? null : reader.GetString(2);
       loadingCourse.CourseType = reader.GetString(3);
       if (!reader.IsDBNull(4))
     loadingCourse.StartDate = reader.GetDateTime(4);
       loadingCourse.LaptopRequired = reader.GetBoolean(5);
       loadingCourse.MaximumParticipants = reader.GetInt32(6);
       if (!reader.IsDBNull(7))
     loadingCourse.Price = reader.GetDouble(7);
       courses.Add(loadingCourse);
     }
     //Cursor ook afsluiten
     reader.Close();
     conn.Close();
       }
       foreach (var item in courses)
     Console.WriteLine(item);
       Console.WriteLine("Gegevens ophalen gelukt");
       Console.ReadLine();
 }