Search code examples
c#unity-game-enginemariadbheidisql

Unity HeidiSQL Connection timed out


In my Unity project i have a MariaDB and HeidiSQL as the interface. I now want to connect my unity script with that database and make queries. In my Database.cs i have following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;

public class Database : MonoBehaviour
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string user;
    private string password;

    public void Database_Initialize()
    {
        server = "IP-ADDRESS";
        database = "DB_NAME";
        user = "USERNAME";
        password = "PASSWORD";

        string connectionString = "Server=IP-ADDRESS;Port=PORT;Database=DB_NAME;Uid=USERNAME;Pwd=PASSWORD;";
        MySqlConnection connection = new MySqlConnection(connectionString);

        connection.Open();

        string query = "INSERT INTO user (UserName) VALUES ('User1')";
        MySqlCommand command = new(query, connection);

        command.CommandTimeout = 60;

        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            // Access data using reader["columnname"]
        }
        reader.Close();

        connection.Close();
    }
}

when i run this script i get following error: TimeoutException: The operation has timed out. Rethrow as MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. MySql.Data.Common.StreamCreator.GetTcpStream (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings, MySql.Data.Common.MyNetworkStream& networkStream) (at <13314bc2115d4bd2a5d896df29fb7179>:0)

Does anyone have an idea on how to fix it?

Thanks in advance

P.S. the MySQL.Data.dll file is from here: https://dev.mysql.com/downloads/connector/net/


Solution

  • It's not clear whether you want to insert a user into the database, read something from the database or both, I'm assuming insert. The changes are described in the comments.

    public void Database_Initialize()
    {
        server = "IP-ADDRESS";
        database = "DB_NAME";
        user = "USERNAME";
        password = "PASSWORD";
    
        // Use interpolated strings to insert the values from variables
        string connectionString = $"Server={server};Port=PORT;Database={database};Uid={user};Pwd={password};";
        // Use "using" pattern to make sure everything is disposed
        using (var connection = new MySqlConnection(connectionString))
        {
            connection.Open();
    
            string query = "INSERT INTO user (UserName) VALUES ('User1')";
            using (MySqlCommand command = new(query, connection))
            {
                command.CommandTimeout = 60;
                // Use ExecuteNonQuery to execute the INSERT.
                // recordsChanged should be 1 afterwards
                int recordsChanged = command.ExecuteNonQuery();
            }
        }