コード例 #1
0
        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);
        }
コード例 #2
0
        // USE JOIN TABLE TO FIND BOOK BY AUTHOR
        // public static Book findBook()
        // {
        //
        // }


        public static List <Author> GetAll()
        {
            List <Author> allAuthors = new List <Author> {
            };
            MySqlConnection conn     = DB.Connection();

            conn.Open();
            MySqlCommand cmd = conn.CreateCommand() as MySqlCommand;

            cmd.CommandText = @"SELECT * FROM authors;";
            MySqlDataReader rdr = cmd.ExecuteReader() as MySqlDataReader;

            while (rdr.Read())
            {
                Author newAuthor = new Author();
                newAuthor.SetId(rdr.GetInt32(0));
                newAuthor.SetName(rdr.GetString(1));
                // newAuthor.SetCopies(rdr.GetInt32(2));
                allAuthors.Add(newAuthor);
            }
            conn.Close();
            if (conn != null)
            {
                conn.Dispose();
            }
            return(allAuthors);
        }