Пример #1
0
        public HTTP_Page ViewPage(int id)
        {
            //Creating an object of class connect using the connectionString
            MySqlConnection Connect = new MySqlConnection(ConnectionString);
            //Creating an object of HTTP_Page which will be returned by the function
            HTTP_Page page = new HTTP_Page();

            //Try block to catch exceptions that may arise from the code.
            try
            {
                //Below query will select the record  with the pageid passed in the parameter.
                string query = "select * from PAGE where pageid = " + id;
                Debug.WriteLine("Connection Initialized...");

                //Below lines open the connection, run the query against the db and get the resultset
                Connect.Open();
                MySqlCommand    cmd       = new MySqlCommand(query, Connect);
                MySqlDataReader resultset = cmd.ExecuteReader();

                //read through the result set
                while (resultset.Read())
                {
                    //To loop across each column in the PAGE table
                    for (int i = 0; i < resultset.FieldCount; i++)
                    {
                        string key   = resultset.GetName(i);
                        string value = resultset.GetString(i);

                        if (key == "pagetitle")
                        {
                            page.SetPageTitle(value);
                        }
                        if (key == "pagebody")
                        {
                            page.SetPageContent(value);
                        }
                        if (key == "pageauthor")
                        {
                            page.SetPageAuthor(value);
                        }
                        if (key == "pagedate")
                        {
                            page.SetPagePublishDate(value);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Writing the error in order to debug
                Debug.WriteLine("Exception in ViewPage(int id) method!");
                Debug.WriteLine(ex.ToString());
            }
            //Below block will execute even when there's an exception.
            finally
            {
                Connect.Close();
                Debug.WriteLine("Database connection closed.");
            }
            //Returning the required page
            return(page);
        }
Пример #2
0
        //ListPages function returns a 'List of HTTP_Page objects'. Using these page objects the records are dynamically displayed.
        public List <HTTP_Page> ListPages(string query)
        {
            MySqlConnection Connect = new MySqlConnection(ConnectionString);

            // Creating a List of HTTP_Page objects, which will be modified and returned by the function.
            List <HTTP_Page> pageList = new List <HTTP_Page>();

            try
            {
                //The below lines opens a connection, passes query to the connection and gets the result set after executing the query.
                Connect.Open();
                MySqlCommand    cmd       = new MySqlCommand(query, Connect);
                MySqlDataReader resultset = cmd.ExecuteReader();

                //Looping through the resultset to get the details needed to display in listPages.
                while (resultset.Read())
                {
                    //Creating page object which will hold  individual record details. This will be added to the list ('pageList').
                    HTTP_Page page = new HTTP_Page();
                    Debug.WriteLine("Reading from the result Set");
                    //Code to get individual record details.
                    for (int i = 0; i < resultset.FieldCount; i++)
                    {
                        string key   = resultset.GetName(i);
                        string value = resultset.GetString(i);

                        if (key == "pageid")
                        {
                            page.SetPageId(Int32.Parse(value));
                        }
                        if (key == "pagetitle")
                        {
                            page.SetPageTitle(value);
                        }
                        if (key == "pagebody")
                        {
                            page.SetPageContent(value);
                        }
                        if (key == "pageauthor")
                        {
                            page.SetPageAuthor(value);
                        }
                        if (key == "pagedate")
                        {
                            page.SetPagePublishDate(value);
                        }
                        if (key == "pagestatus")
                        {
                            page.SetPagePublishStatus(bool.Parse(value));
                        }
                    }
                    //Adding an individual page record to the pageList.
                    pageList.Add(page);
                }
                resultset.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("An exception occured in the ListPages(String query) method! ");
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                //Closing the connection
                Connect.Close();
            }
            Debug.WriteLine("Database Connection Closed.");
            //Returning pageList which has a list of HTTP_Page objects.
            return(pageList);
        }