skip to Main Content

I’m one ear in self learning C# and Unity. Currently I’m in casual development of game. I want to store data in database. I have latest version of Visual Studio and MySQL Server Management Studio.

I succesfuly connected my local DB to VS. I’m able to create tables and add parameters manualy, but I’m not able to comunicate with DB via code itself. The code (below) compile without issue. It seems that I have an error in my connectionString, but I triplececked it.

Can you kick me in the right direction?

Screenshot of succesful connection of DB to VS

using System;
using System.Data;
using MySql.Data.MySqlClient;

public class MySQLConnector
{
    public static void Main(string[] arg)
    {
        string connectionString =
            "Server=*servername*;" +
            "Database=*DBname*;" +
            "Uid=AnyBaddy;" 
            ;
        MySqlConnection connection = new MySqlConnection(connectionString);
        try
        {
            connection.Open();
            Console.WriteLine("Connection successful!");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            connection.Close();
        }
    }
}

I tryed to create multiple diferent users with different permisions.
I tryed to re/create multiple servers.
I tryed Postgresql instead of SSMS.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I changed my code to:

    using Microsoft.Data.SqlClient;
    
    public class SQLConnector
    {
        public static void Main(string[] arg)
        {
            string connectionString =
                "Data Source=BaddyPC;" +
                "Initial Catalog=dbname;" +
                "User id=AnyBaddy;" +
                "Password=userpassword;";
            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();
                Console.WriteLine("Connection successful!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    }
    

    After certificate issue i tryed suggestion from comments: How to fix SQL Server 2019 connection error due to certificate issue

    Still have the certificate problem.


  2. Add import java.mysql.drivermanager
    Add import java.mysql.connection
    And download mysql jar from chrome paste it in application
    Then you are good to go

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search