reviews2

Databases are things that store stuff. At a high-level at least. They are typically stored on the internet, but can also be hosted in local files and such.

there are many kinds of databases, some of them, like CSVs, are simple text based files that store stuff in columns and rows; while others are complex systems that are secure and safe, the most popular of this being SQL or Structared Query Language.

To connect to a SQL data base, you often need to perform hosting services and fancy, often frusterating, coding tasks.

In my program Flash Card Database, I used a library called MySQL to connect to and perform queries on my database. One of the more basic functions looked like this:

public AvaloniaList GetCards()
{
    var sqlQuery = "SELECT * FROM Card";

    AvaloniaList cards = new AvaloniaList();
    try
    {
        using (MySqlConnection conn = new MySqlConnection(_connectionString))
        {
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                var id = int.Parse(reader[0].ToString() ?? "0");
                var numRight = int.Parse(reader[4].ToString() ?? "0");
                var numWrong = int.Parse(reader[5].ToString() ?? "0");
                var card = new Card(id, reader[1].ToString() ?? "0", reader[2].ToString() ?? "0", reader[3].ToString() ?? "0", numRight, numWrong);
                cards.Add(card);
            }

            conn.Close();
        }
    }
    catch (MySqlException ex)
    {
        throw ex;
    }

    return cards;
}
                          

In this function, I open a connection to the database and execute the query SELECT * FROM Card. This simply means that I get every column from the Card table. I then perform various actions to parse and load that data into a usable manner.

In another one of my functions, I give myself the ability to add cards to the dataset.

public void AddCard(Card card)
{
    var sqlQuery = "INSERT INTO Card (Title, Question, Answer, NumRight, NumWrong) VALUES(@Title, @Question, @Answer, @NumRight, @NumWrong);";
    try
    {
        using (MySqlConnection conn = new MySqlConnection(_connectionString))
        {
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);
            cmd.Parameters.AddWithValue("@Title", card.Title);
            cmd.Parameters.AddWithValue("@Question", card.Question);
            cmd.Parameters.AddWithValue("@Answer", card.Answer);
            cmd.Parameters.AddWithValue("@NumRight", card.NumRight);
            cmd.Parameters.AddWithValue("@NumWrong", card.NumWrong);
            int rowsReturned = cmd.ExecuteNonQuery();
            Console.WriteLine("{0} rows returned.", rowsReturned);
            conn.Close();
        }
    }
    catch (MySqlException ex)
    {
        throw ex;
    }
}
                          

In this function, I use the query INSERT INTO Card (Title, Question, Answer, NumRight, NumWrong) VALUES(@Title, @Question, @Answer, @NumRight, @NumWrong); This function connects to the database and adds various varibales into a command object. The command object then executs that SQL query to make a new rom in the Card table that contains all of the variable's values that I inserted into the command.

This, and a few more functions, gives me full functionality to create, remove, and get cards form a secure database.

Thank you for reading this far about databases, you can find the source here.