public static List <Author> GetAll() { List <Author> allAuthors = new List <Author> { }; MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM authors;"; MySqlDataReader rdr = cmd.ExecuteReader() as MySqlDataReader; while (rdr.Read()) { int authorId = rdr.GetInt32(0); string authorName = rdr.GetString(1); Author newAuthor = new Author(authorName); newAuthor.SetId(authorId); allAuthors.Add(newAuthor); } conn.Close(); if (conn != null) { conn.Dispose(); } return(allAuthors); }
public static Author Find(int check) { Author author = new Author(); MySqlConnection conn = DB.Connection(); conn.Open(); MySqlCommand cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM authors where id = " + check + ";"; MySqlDataReader rdr = cmd.ExecuteReader() as MySqlDataReader; //nested if in a while loop to eliminate an error that was saying "Read must be done first" while (rdr.Read()) { if (rdr.IsDBNull(0) == false) { author.SetId(rdr.GetInt32(0)); author.SetName(rdr.GetString(1)); // author.SetCopies(rdr.Getint(2)); } } conn.Close(); if (conn != null) { conn.Dispose(); } return(author); }
public static Author CheckDuplicate(Author checkAuthor) { List <Author> allAuthors = Author.GetAll(); bool isInDB = false; foreach (Author author in allAuthors) { if (author.GetFirstName() == checkAuthor.GetFirstName() && author.GetLastName() == checkAuthor.GetLastName()) { isInDB = true; checkAuthor.SetId(author.GetId()); } } if (!isInDB) { checkAuthor.Save(); } return(checkAuthor); }