Beispiel #1
0
        //This code is referenced from SCHOOLDB.cs in our examples
        public List <HTTP_Page> List_Query(string query)
        {
            //Set up SQL Connection
            MySqlConnection Connect = new MySqlConnection(ConnectionString);

            //Our list of HTTP_Page that is the result from the query
            List <HTTP_Page> ResultSet = new List <HTTP_Page>();

            try
            {
                Debug.WriteLine("Connection Initialized...");
                Debug.WriteLine("Attempting to execute query " + query);
                //open the db connection
                Connect.Open();
                //give the connection a query
                MySqlCommand cmd = new MySqlCommand(query, Connect);
                //grab the result set
                MySqlDataReader resultset = cmd.ExecuteReader();

                //for every row in the result set
                while (resultset.Read())
                {
                    //Create a new HTTP_Page object
                    HTTP_Page Row = new HTTP_Page();
                    //for every column in the row
                    for (int i = 0; i < resultset.FieldCount; i++)
                    {
                        //Switch statement to set the value to their correct column
                        switch (resultset.GetName(i))
                        {
                        case "HTTPPAGEID":
                            Row.SetID(resultset.GetString(i));
                            break;

                        case "HTTPPAGETITLE":
                            Row.SetTitle(resultset.GetString(i));
                            break;

                        case "HTTPPAGECONTENT":
                            Row.SetContent(resultset.GetString(i));
                            break;

                        case "HTTPPAGEDATE":
                            Row.SetDate(DateTime.Parse(resultset.GetString(i)));
                            break;

                        default:
                            Debug.WriteLine("The column name is not in the HTTP_Page");
                            break;
                        }
                    }
                    //Add the object to the list result
                    ResultSet.Add(Row);
                }//end row

                //Finish reading (no more content left)
                resultset.Close();
            }
            //Exception occured
            catch (Exception ex)
            {
                Debug.WriteLine("Something went wrong in the List_Query method!");
                Debug.WriteLine(ex.ToString());
            }
            //Close the connection to the database
            Connect.Close();
            Debug.WriteLine("Database Connection Terminated.");

            //Return our result
            return(ResultSet);
        }
Beispiel #2
0
        //This code is referenced from SCHOOLDB.cs in our examples
        public HTTP_Page FindPage(int id)
        {
            //Utilize the connection string
            MySqlConnection Connect = new MySqlConnection(ConnectionString);

            //Create a new HTTP_Page Object to be used for are return value
            HTTP_Page result_page = new HTTP_Page();

            //we will try to grab HTTP_Page data from the database, if we fail, a message will appear in Debug>Windows>Output dialogue
            try
            {
                //Build a custom query with the id information provided
                string query = "select * from http_pages where HTTPPAGEID = " + id;
                Debug.WriteLine("Connection Initialized...");
                //open the db connection
                Connect.Open();
                //Run out query against the database
                MySqlCommand cmd = new MySqlCommand(query, Connect);
                //grab the result set
                MySqlDataReader resultset = cmd.ExecuteReader();

                //Create a list of pages (although we're only trying to get 1)
                List <HTTP_Page> pages = new List <HTTP_Page>();

                //read through the result set
                while (resultset.Read())
                {
                    //information that will store a single page
                    HTTP_Page currentPage = new HTTP_Page();

                    //Look at each column in the result set row, add both the column name and the column value to our Student dictionary
                    for (int i = 0; i < resultset.FieldCount; i++)
                    {
                        string key   = resultset.GetName(i);
                        string value = resultset.GetString(i);
                        Debug.WriteLine("Attempting to transfer " + key + " data of " + value);
                        //Switch statement to set the value to their correct column
                        switch (key)
                        {
                        case "HTTPPAGEID":
                            currentPage.SetID(value);
                            break;

                        case "HTTPPAGETITLE":
                            currentPage.SetTitle(value);
                            break;

                        case "HTTPPAGECONTENT":
                            currentPage.SetContent(value);
                            break;

                        case "HTTPPAGEDATE":
                            currentPage.SetDate(DateTime.Parse(value));
                            break;

                        default:
                            Debug.WriteLine("The column name is not in the HTTP_Page");
                            break;
                        }
                    }
                    //Add the page to the list of pages
                    pages.Add(currentPage);
                }
                //get the first page
                result_page = pages[0];
            }
            //An exception occured
            catch (Exception ex)
            {
                //If something (anything) goes wrong with the try{} block, this block will execute
                Debug.WriteLine("Something went wrong in the find Page method!");
                Debug.WriteLine(ex.ToString());
            }
            //Close the database connection
            Connect.Close();
            Debug.WriteLine("Database Connection Terminated.");

            //Return our result
            return(result_page);
        }