There was a problem loading the comments.

How can I connect to a MySQL database through .NET?

Support Portal  »  Knowledgebase  »  Viewing Article

MySQL Connection Strings on a Windows hosted site
All Windows shared servers have the Connector/Net 6.0 connector, MySQL's fully managed ADO.Net provider, installed. The current version we use is 6.0.4.

Documentation on this connector is available from , however for convenience, here are a few common examples of connection strings you can use:


Specifying TCP port
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername; Pwd=myPassword;
The port 3306 is the default MySql port.

The value is ignored if Unix socket is used.

Multiple servers
Use this to connect to a server in a replicated server configuration without concern on which server to use. Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase; Uid=myUsername;Pwd=myPassword;

Using encryption
This one activates SSL encryption for all data sent between the client and server. The server must have a certificate installed. Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; Encrypt=true;

Disallow batches
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; AllowBatch=False;

Allow User Variables
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; AllowUserVariables=True;

Allow Invalid Date/Time 1
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; AllowZeroDateTime=True;
Returns a MySqlDateTime object for invalid values and a System.DateTime object for valid values.

Allow Invalid Date/Time Alternative
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; ConvertZeroDateTime=True;
Returns System.DateTime.MinValue valued System.DateTime object for invalid values and a System.DateTime object for valid values.

Disable transaction participation
The use of auto enlist transactionscope (default behaviour) could cause trouble in medium trust environments. Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; AutoEnlist=False;

Skip parameter checks for stored routines
Default behaviour is that parameters for stored routines (stored procedures) are checked against the server Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CheckParameters=False;

Some permissions and value casting related errors reported fixed when using this connection option.
Skip parameter type and order matching for stored procedures Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; UseProcedureBodies=False;

The default behaviour is to read tables mysql.proc/INFORMATION_SCHEMA.ROUTINES and try to map provided command parameter values to the called procedures parameters and type cast values accordingly.

This can be an issue if permissions to the earlier mentioned sproc info tables are not sufficient.

The driver will not automatically map the parameters so you must manually set parameter types and you must also make sure to add the parameters to the command object in the exact order as appeared in the procedure definition.

Count changed rows rather than found rows
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; UseAffectedRows=True;

Compress network communication between client and server
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; UseCompression=True;

Log inefficient database operations
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; UseUsageAdvisor=True;

Enable performance counters
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; UsePerformanceMonitor=True;

Share via
Did you find this article useful?  

Related Articles

© Eco Web Hosting