Example #1
0
        /*
         * Programmer: Megan Villwock
         * Last Modified Date: 10/30/2018
         *
         * Gets data from the database and stores it in class/objects.
         *
         */

        public static bool GetTestSessions(List <TestSession> testSessionList, ref string error)
        {
            List <TestSession> TestSessionList = new List <TestSession>();

            SqlConnection Connection = new SqlConnection();
            SqlDataReader TestSessionDataReader;
            SqlCommand    Command;
            TestSession   testSession;

            try
            {
                // Connect to database and get data.
                Connection = DatabaseHelper.Connect();
                Connection.Open();
                Command               = new SqlCommand();
                Command.Connection    = Connection;
                Command.CommandText   = "SELECT SessionID, TestID, UserID, CreationDate FROM TestSessions;";
                TestSessionDataReader = Command.ExecuteReader();

                // Add data to list.
                while (TestSessionDataReader.Read())
                {
                    testSession = new TestSession();
                    testSession.intSessionID         = TestSessionDataReader.GetInt32(0);
                    testSession.intTestID            = TestSessionDataReader.GetInt32(1);
                    testSession.intUserID            = TestSessionDataReader.GetInt32(2);
                    testSession.datetimeCreationDate = TestSessionDataReader.GetDateTime(3);

                    testSessionList.Add(testSession);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                return(false);
            }
            finally
            {
                if (Connection != null)
                {
                    Connection.Close();
                }
            }
        }
Example #2
0
        public static bool GetTestDate(List <TestSession> dateList, ref string error, int UserID, int TestID)
        {
            List <TestSession> TestSessionList = new List <TestSession>();

            SqlConnection Connection = new SqlConnection();
            SqlDataReader TestSessionDataReader;
            SqlCommand    Command;
            TestSession   testsession;

            try
            {
                // Connect to database and get data.
                Connection = DatabaseHelper.Connect();
                Connection.Open();
                Command             = new SqlCommand();
                Command.Connection  = Connection;
                Command.CommandText = "SELECT TestSessions.TestID, TestSessions.CreationDate FROM TestSessions" +
                                      "                  WHERE TestSessions.UserID = @UserID AND TestSessions.TestID = @TestID;";
                Command.Parameters.AddWithValue("@UserID", UserID);
                Command.Parameters.AddWithValue("@TestID", TestID);
                TestSessionDataReader = Command.ExecuteReader();

                // Add data to list.
                while (TestSessionDataReader.Read())
                {
                    testsession                      = new TestSession();
                    testsession.intTestID            = TestSessionDataReader.GetInt32(0);
                    testsession.datetimeCreationDate = TestSessionDataReader.GetDateTime(1);
                    dateList.Add(testsession);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                return(false);
            }
            finally
            {
                if (Connection != null)
                {
                    Connection.Close();
                }
            }
        }