public Authors FindAuthor(int id) { MySqlConnection Connect = new MySqlConnection(ConnectionString); Authors result_author = new Authors(); try { string query = "select * from authors where authorid = " + id; Debug.WriteLine("Connection Initialized..."); Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); MySqlDataReader resultset = cmd.ExecuteReader(); List <Authors> authors = new List <Authors>(); while (resultset.Read()) { Authors currentauthor = new Authors(); 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 (key) { case "authorfname": currentauthor.SetAuthorfname(value); break; case "authorlname": currentauthor.SetAuthorlname(value); break; } } authors.Add(currentauthor); } result_author = authors[0]; } catch (Exception ex) { Debug.WriteLine("Something went wrong in the find Author method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); Debug.WriteLine("Database Connection Terminated."); return(result_author); }
public void Update_Author(int authorid, Authors new_author) { string query = "update authors set authorfname='{0}', authorlname='{1}' where authorid={2}"; query = String.Format(query, new_author.GetAuthorfname(), new_author.GetAuthorlname(), authorid); MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { Connect.Open(); cmd.ExecuteNonQuery(); Debug.WriteLine("Executed query " + query); } catch (Exception ex) { Debug.WriteLine("Something went wrong in the Update_Author Method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); }
//adding a new author public void Add_Author(Authors new_author) { string query = "insert into authors (authorfname, authorlname) values('{0}', '{1}')"; query = String.Format(query, new_author.GetAuthorfname(), new_author.GetAuthorlname()); MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Debug.WriteLine("Something went wrong in the Add_Author Method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); }