Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Task - Executing MySQL Commands | Introduction to Databases with .NET
Introduction to .NET with C#

Task - Executing MySQL CommandsTask - Executing MySQL Commands

Your task is to:

  • Create a MySqlCommand object using the commandText command and the connection object;
  • Execute the command using the appropriate method;
  • Close the connection after executing the command;
  • Enclose the code into a try-catch block to make it safer.

Note

The unit tests related to MySQL tasks will only pass if the application connects to an actual database, so for this task, you need to have the MySQL database installed on your device. You can modify the connection string to your local database details for testing purposes. Alternatively, you can compare your code with the given solution code.

Link to the Task: GitHub
1. The command is a non-query so the suitable method for executing it will be ExecuteNonQuery.
2. You can either use the Close method of the MySqlConnection object to close the connection, or you can utilize the using statement.
using MySql.Data.MySqlClient;

public class MySqlCreateDB
{
    // You can modify the connection string here
    public static string connectionString = "server=your_host;port=xxxx;user=your_user;password=your_pass;";

    public static void Main(string[] args)
    {
        CreateDatabase();
    }

    public static void CreateDatabase()
    {
        // Modify code below this line
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            try
            {
                connection.Open();

                Console.WriteLine("Successfully Connected To The MySQL Server");

                string commandText = "CREATE DATABASE IF NOT EXISTS office";
                var command = new MySqlCommand(commandText, connection);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        // Modify code above this line
    }
}     

Все було зрозуміло?

Секція 3. Розділ 7
course content

Зміст курсу

Introduction to .NET with C#

Task - Executing MySQL CommandsTask - Executing MySQL Commands

Your task is to:

  • Create a MySqlCommand object using the commandText command and the connection object;
  • Execute the command using the appropriate method;
  • Close the connection after executing the command;
  • Enclose the code into a try-catch block to make it safer.

Note

The unit tests related to MySQL tasks will only pass if the application connects to an actual database, so for this task, you need to have the MySQL database installed on your device. You can modify the connection string to your local database details for testing purposes. Alternatively, you can compare your code with the given solution code.

Link to the Task: GitHub
1. The command is a non-query so the suitable method for executing it will be ExecuteNonQuery.
2. You can either use the Close method of the MySqlConnection object to close the connection, or you can utilize the using statement.
using MySql.Data.MySqlClient;

public class MySqlCreateDB
{
    // You can modify the connection string here
    public static string connectionString = "server=your_host;port=xxxx;user=your_user;password=your_pass;";

    public static void Main(string[] args)
    {
        CreateDatabase();
    }

    public static void CreateDatabase()
    {
        // Modify code below this line
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            try
            {
                connection.Open();

                Console.WriteLine("Successfully Connected To The MySQL Server");

                string commandText = "CREATE DATABASE IF NOT EXISTS office";
                var command = new MySqlCommand(commandText, connection);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        // Modify code above this line
    }
}     

Все було зрозуміло?

Секція 3. Розділ 7
some-alt