public void UpdatePage(int pageid, HTTP_Page new_page) { //SQL query to update the PAGE table with the new values. string query = "update PAGE set PAGETITLE='{0}', PAGEBODY='{1}', PAGEAUTHOR='{2}', PAGESTATUS='{3}' where PAGEID = '{4}'"; //To update values for PAGESTATUS field which is of type boolean. 0 is inserted if PAGESTATUS is false, 1 if true. int page_publish_value = 0; if (new_page.GetPagePublishStatus()) { page_publish_value = 1; } //Getting data from 'new_page' object which is passed in the parameter of the function during the function call. ('db.AddPage(new_page)') query = String.Format(query, new_page.GetPageTitle(), new_page.GetPageContent(), new_page.GetPageAuthor(), page_publish_value, pageid); //Creating a new connection object using the connection string and Creating an object for a MySqlCommand using query and the connect object. MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { //Opening the connection and executing the query on the database. Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Debug.WriteLine("Exception in the updatePage(int pageId, HTTP_Page page) method!"); Debug.WriteLine(ex.ToString()); } finally { //Database connection is closed. Connect.Close(); } }
public void AddPage(HTTP_Page new_page) { //SQL query to insert data into the PAGE table string query = "insert into PAGE (PAGETITLE,PAGEBODY,PAGEDATE,PAGESTATUS,PAGEAUTHOR) values ('{0}','{1}',now(),'{2}','{3}')"; //To insert values for PAGESTATUS field which is of type boolean. 0 is inserted if PAGESTATUS is false, 1 if true. int page_publish_value = 0; if (new_page.GetPagePublishStatus()) { page_publish_value = 1; } //Getting data from 'new_page' object which is passed in the parameter of the function during the function call. ('db.AddPage(new_page)') query = String.Format(query, new_page.GetPageTitle(), new_page.GetPageContent(), page_publish_value, new_page.GetPageAuthor()); //Creating a new connection object using the connection string and Creating an object for a MySqlCommand using query and the connect object. MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); //try catch and finally for exception handling try { Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Debug.WriteLine("Exception in the AddPage(HTTP_Page new_page) method"); Debug.WriteLine(ex.ToString()); } finally { Connect.Close(); } }