MySql Database Connection String with C# code snippets

In this article, find out list of MySql database connection string with C# code snippets. In this article you can see how to create connector.NET connection string for MySql database. You can find the MySql database connection string template with all the configuration parameters.


MySql Database Connection String in C#:

Connection String provide the required information to the driver that tells where to find the default connection information. Optionally, you are allowed to specify attribute=value pairs in the connection string to override the default values stored in the data source.

1. Standard or basic MySql connection string:

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;";
conn.ConnectionString = sConnString;
conn.Open();
.

Server: Specify the server name where the MySQL database is located. You can enter the IP address as the server name.
Database: Specify the name of the MySql database.
Uid: Specify the username.
Pwd: Specify the password

For instance, if your MySql database is located in your local computer then you can write the connection string like this below.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=127.0.0.1;Database=StudentDB;Uid=john;Pwd=john@123;";
conn.ConnectionString = sConnString;
conn.Open();
.

2. Standard or basic MySql connection string with multiple servers:

Use the following connection string template when you have MySql databse is located in different servers.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress1,myServerAddress2,myServerAddress3;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;";
conn.ConnectionString = sConnString;
conn.Open();
.

3. Standard or basic MySql connection string with TCP port:

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Port=1001;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;";
conn.ConnectionString = sConnString;
conn.Open();
.

Port: Specify the port name. The default port number is set to 3306 if the Port property is not included in the connection string. Also, note that Port value is ignored if you have used Unix socket.

4. Standard or basic MySql connection string with SSL Encryption

The server must have installed a certificate in order to use SSL encrypted data to be set send across client and server.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;Encrypt=true";
conn.ConnectionString = sConnString;
conn.Open();
.

Encrypt: The Encrypt property enable SSL encryption data transfer between client and server. This configuration property is available from .NET version 5.0.3 to 6.2.1.


.
//Prefered encryption
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;SslMode=Preferred";
conn.ConnectionString = sConnString;
conn.Open();

OR

//Forced Encryption
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;SslMode=Required";
conn.ConnectionString = sConnString;
conn.Open();
.

SslMode: The Encrypt configuration property is replaced with SslMode from .NET version 6.2.1 version onwards.
SslMode=Preferred: Preferred encryption.
SslMode=Required: Forced encryption.


You can also have MySql connection string with SSL specific personal store based certificate. Here is the connection string template.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;SslMode=Required;Certificate Store Location=CurrentUser;";
conn.ConnectionString = sConnString;
conn.Open();
.

Certificate Store Location: The certificate store location name. This configuration is available from .NET version 6.2.1


You can also have MySql connection string with SSL thumbprint specific personal store based certificate. Here is the connection string template.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;SslMode=Required;Certificate Store Location=CurrentUser;Certificate Thumbprint=2ef067c1a9958f24f2dda997e20946990453c;";
conn.ConnectionString = sConnString;
conn.Open();
.

Certificate Thumbprint: The certificate thumbprint text. This configuration is available from .NET version 6.2.1


You can also have MySql connection string with SSL file based certificate. Here is the connection string template.

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;SslMode=Required;CertificateFile=C:\MyDir\myCertificate.pfx;CertificatePassword=password;";
conn.ConnectionString = sConnString;
conn.Open();
.

CertificateFile: The absolute path of certificate file location.
CertificatePassword: The certificate password.
This configuration is available from .NET version 6.2.1 onwards.


5. MySql connection string with Disallow batch:

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;AllowBatch=False";
conn.ConnectionString = sConnString;
conn.Open();
.

6. MySql connection string with Allow User Variables:

.
using MySql.Data.MySqlClient;

MySqlConnection conn = MySqlConnection();
string sConnString = "Server=myServerAddress;Database=myDatabaseName;Uid=myUserName;Pwd=myPassword;AllowUserVariables=True";
conn.ConnectionString = sConnString;
conn.Open();
.

1. ODBC DSN

using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=DsnName;" +
"Uid=UserName;" +
"Pwd=Secret;";
conn.Open();

2. ODBC – MyODBC Driver (local database)

using System.Data.Odbc;
.
var conn = new OdbcConnection();
conn.ConnectionString = 
            "Driver={MySql};" + 
            "Server=localhost;" + 
            "Option=16834;" + 
            "DataBase=DataBaseName;" 
conn.Open();
.

3. ODBC – MyODBC Driver (remote database)

using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = 
            "Driver={MySql};" + 
            "Server=db.domain.com;" + 
            "Option=131072;" + 
            "Port=3306;" + 
            "Stmt=;" + 
            "DataBase=DataBaseName;" + 
            "Uid=UserName;" + 
            "Pwd=Secret;" 
conn.Open();

4. ODBC – MySQL ODBC 3.51 Driver

using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = 
     "Driver={MySql ODBC 3.51 Driver};" + 
     "Server=ServerName;" + 
     "Option=16834;" + 
     "Port=3306;" + 
     "Stmt=;" + 
     "DataBase=DataBaseName;" + 
     "Uid=UserName;" + 
     "Pwd=Secret;" 
conn.Open();
// or 
var conn = new OdbcConnection();
conn.ConnectionString = 
     "DRIVER={MySql ODBC 3.51 Driver};" + 
     "SERVER=ServerName;" + 
     "DATABASE=DataBaseName;" + 
     "USER=UserName;" + 
     "PASSWORD=Secret;" 
conn.Open();

5. OleDb

using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString = 
            "Provider=MySqlProv;" + 
            "Data Source=ServerName;" + 
            "User id=UserName;" + 
            "Password=Secret;" 
conn.Open();

6. .NET DataProvider from CoreLab

using CoreLab.MySql;
var conn = new MySqlConnection();
conn.ConnectionString = 
              "Host=ServerName;" + 
              "DataBase=DataBaseName;" + 
              "Protocol=TCP;" + 
              "Port=3306;" + 
              "Direct=true;" + 
              "Compress=false;" + 
              "Pooling=true;" + 
              "Min Pool Size=0;" + 
              "Max Pool Size=100;" + 
              "Connection Lifetime=0;" + 
              "User id=UserName;" + 
              "Password=Secret;" + 
conn.Open();

– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   C# Performance Improvement Tips - By using ? operator
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo