Search code examples
c#mysql-connector

Connection string for MySQL in C#


I'm trying to connect to MySQL, implementing a Web Application using C# and ASP.Net

Currently, I'm connecting to my DB in localhost using the default port 3306 I'm using the following connection string, this is for version 5.7.2.2

"server=localhost;user id=root;Pwd=Mypwd; persistsecurityinfo=True;database=cc;convert zero datetime=True"

Now, I want to use version 8.0.2.5 For this version, I use server 127.0.0.1 and port 3320

I tried changing the connection string to:

"server=127.0.01;user id=root;Pwd=Mypwd; persistsecurityinfo=True;database=cc;convert zero datetime=True"

And also:

"server=localhost;port:3320;user id=root;Pwd=Mypwd; persistsecurityinfo=True;database=cc;convert zero datetime=True"

But they didn't work, it's not connecting

I found this example on https://www.connectionstrings.com/mysql/:

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

But that doesn't work either.

Do you know another way, or what am I missing?


Solution

  • The below connection string might be helpful for your problem.

    "server=localhost:3320;uid=root;pwd=Mypwd;database=cc;persistsecurityinfo=true;convertzerodatetime=true"
    

    Else remove persistsecurityinfo and convertzerodatetime then try.

    "server=localhost:3320;uid=root;pwd=Mypwd;database=cc"
    

    Refer this too in MySQL documentation.

    // Sessions
    MySQLX.GetSession($"mysqlx://user@host/schema")
    MySQLX.GetSession($"mysqlx://user@host/schema?connection-attributes")
    MySQLX.GetSession($"mysqlx://user@host/schema?connection-attributes=true")
    MySQLX.GetSession($"mysqlx://user@host/schema?connection-attributes=false")
    MySQLX.GetSession($"mysqlx://user@host/schema?connection-attributes=[attr1=val1,attr2,attr3=]")
    MySQLX.GetSession($"mysqlx://user@host/schema?connection-attributes=[]")
    
    // Pooling
    MySQLX.GetClient($"mysqlx://user@host/schema")
    MySQLX.GetClient($"mysqlx://user@host/schema?connection-attributes")
    MySQLX.GetClient($"mysqlx://user@host/schema?connection-attributes=true")
    MySQLX.GetClient($"mysqlx://user@host/schema?connection-attributes=false")
    MySQLX.GetClient($"mysqlx://user@host/schema?connection-attributes=[attr1=val1,attr2,attr3=]")
    MySQLX.GetClient($"mysqlx://user@host/schema?connection-attributes=[]")
    

    Reference: https://dev.mysql.com/doc/connector-net/en/connector-net-8-0-connection-options.html