17MariaDB in C#

This example demonstrates the MariaDB database in a C# application. If you skipped Chapter 16, “MariaDB in Python,” which built a similar example in Python, go to that chapter and read the beginning and the first two sections, which are described in the following list:

  • “Install MariaDB” explains how to install MariaDB.
  • “Run HeidiSQL” explains how to use the HeidiSQL database management tool to create a MariaDB database.

When you reach the section “Create the Program” in Chapter 16, return to this chapter and read the following sections.

CREATE THE PROGRAM

To create a C# program to work with the MariaDB extraterrestrial animals database, create a new C# Console App (.NET Framework) and then add the code described in the following sections.

Jupyter Notebook lets you execute cells individually, but C# won't let you do that. It will, however, let you group related code into methods, so that's what we'll do here.

Add code to the main method so that it looks like the following:

using MySqlConnector;

static void Main(string[] args)
{
    CreateDatabase();
    // CreateTables();
    // CreateData();
    // FetchData();
 
    Console.ReadLine();
}

Initially, the main method only calls CreateDatabase (described shortly) and then waits for the user to press Enter. You'll uncomment the other statements as you work through the following sections, which explain how the other methods work.

Install MySqlConnector

To install a database connector, open the Project menu and select Manage ...

Get Beginning Database Design Solutions, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.