using MySql.Data.MySqlClient; is not working

Viewed 116694

Before I used MS SQL but in a new project I use mysql and when I run our application I get this error

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 4:  using System.Text;
Line 5:  using System.Web;
Line 6:  `using MySql.Data.MySqlClient;    this namespace is not working 
Line 7:  using System.Data.SqlTypes;

How can I solve this problem?

13 Answers

In my case, the problem was MySql.Data version. I changed it from 8.0.20 to 6.10.9 (in Managing NuGet Packages) and it started working.

Even if you already have included MySql connector using the references it still might not work. The reason is you might be running on an older version but the connector installed is not compatible with the older version. So go to references -> manage Nuget packages. Then in that window search for mysql.data. Then keep adjusting the version number until it lands on the correct version that is compatible!!!!!!

In case of dotnet core 3.0

  1. Add Mysql.Data Package using NuGET Package Manager
  2. in appsettings.json write connection string for example:

    "AllowedHosts": "*", "ConnectionStrings": { "Default": "server=localhost;user=root;password='123';database=DbName" }

  3. in Startup.cs file->in ConfigureServices method Add

    services.AddTransient(_ => new MySqlConnection(Configuration["ConnectionStrings:Default"]));

It is working fine in my application

Related