I can't connect to a MySQL database from C#. Upon trying to run the executable it spits out this error (this is the beginning of it):
Unhandled Exception: System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40) Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40) at System.Data.SqlClient.SqlInternalConnectionTds..ctor (System.Data.ProviderBase.DbConnectionPoolIdentity identity, System.Data.SqlClient.SqlConnectionString connectionOptions, System.Data.SqlClient.SqlCredential credential, System.Object providerInfo, System.String newPassword, System.Security.SecureString newSecurePassword, System.Boolean redirectedUserInstance, System.Data.SqlClient.SqlConnectionString userConnectionOptions, System.Data.SqlClient.SessionData reconnectSessionData, System.Boolean applyTransientFaultHandling, System.String accessToken) [0x0018b] in <2ebdad619de74d1389f27154469c7cb1>:0 at System.Data.SqlClient.SqlConnectionFactory.CreateConnection (System.Data.Common.DbConnectionOptions options, System.Data.Common.DbConnectionPoolKey poolKey, System.Object poolGroupProviderInfo, System.Data.ProviderBase.DbConnectionPool pool, System.Data.Common.DbConnection owningConnection, System.Data.Common.DbConnectionOptions userOptions) [0x00159] in <2ebdad619de74d1389f27154469c7cb1>:0
I can connect to the database from MySQL Workbench just fine.
I tried using Connector/Net but to no avail.
This is the code:
dbCon = new SqlConnection(
"Data Source=192.168.0.104, 3306; " +
"Initial Catalog=Mundus; " + // Mundus is the database that I am trying to connect to
"User ID=root;" +
"Password=[password that I use];"
);
dbCon.Open(); // from here it breaks
using(dbCon) {
}
My problem was that I was trying to use System.Data.SqlClient
to connect to a MySQL database (it is for Microsoft SQL databases).
Following this StackOverflow comment I used MySql.Data.MySqlClient
to connect. This format of the connection string worked for me:
"server=localhost;" +
"port=3306;" +
"user id=root; " +
"password=[my password]; " +
"database=[the database I want to connect to]; " +
"SslMode=none"