I have to connect my Database to an existing code in .NET. My Database is a MySql DB in PhpMyAdmin.
I am using System.Data.SqlClient
in .NET and it seems like I cant get the connection String right for the connection function
private string _connetionString = @"Data Source=localhost;Initial Catalog=Testdb;"
SqlConnection _connection;
public DatabaseService()
{
_connection = new SqlConnection(_connetionString);
}
Everytime I start my .Net-Service it needs a long time to and eventually the connection fails.
2
Answers
SqlConnection
class (the whole namespaceSystem.Data.SqlClient
) is for connection to Microsoft SQL Server. You are using MySQL, so you need to useSystem.Data.OleDb
namespace (OleDbConnection).How connestion string for MySql look you can check here: https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/
That’s not the right connection string syntax for MySQL. The sample you’ve posted looks to be for MSSQL. For MySQL you would need something like:
"server=localhost;uid=username;pwd=password;database=Testdb"
MySQL API Reference for .NET