0

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) { 
            
}
Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
Syndamia
  • 41
  • 2
  • 9
  • 2
    `System.Data.SqlClient` is for Microsoft SQL Server, not MySQL. What's the error when you switched to Connector/Net? – Lex Li May 09 '20 at 18:16
  • @LexLi Thank you very much, I am an idiot. I didn't realize that `System.Data.SqlClient` is for Microsoft SQL and not MySQL. I will update post with solution. My problem with Connector/Net is that I got a bunch of assembly reference errors ```csharp Program.cs(1,7): error CS0246: The type or namespace name `Gtk' could not be found. Are you missing an assembly reference? Program.cs(2,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `Mundus'. Are you missing an assembly reference? ``` – Syndamia May 10 '20 at 05:24
  • 1
    When you found a solution, post it as an answer and accept it. For other issues, open new questions. – Lex Li May 10 '20 at 14:27

1 Answers1

0

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"
Syndamia
  • 41
  • 2
  • 9