skip to Main Content

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


  1. SqlConnection class (the whole namespace System.Data.SqlClient) is for connection to Microsoft SQL Server. You are using MySQL, so you need to use System.Data.OleDb namespace (OleDbConnection).

    How connestion string for MySql look you can check here: https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search