Reputation: 511
I'm trying to connect to my local database - which I can access using localhost/phpmyadmin - using ASP.NET Core. I'm new to the language, and I am struggling with a MySQL-related exception.
string constr = Configuration.GetSection("ConnectionStrings:con").Value;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = @"SELECT id
FROM `test`";
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
con.Open();
using (var sqlDataReader = cmd.ExecuteReader())
{
while (sqlDataReader.Read())
{
var id = sqlDataReader.GetInt32(0);
}
}
con.Close();
}
}
The exception happens at the line using (var sqlDataReader = cmd.ExecuteReader()).
This is my connection string:
"ConnectionStrings": {
"con": "Server=localhost;Database=musense;User=root;Password=********;Charset=utf8"
}
I am setting the connection string from within the appsettings.json. Without the charset, I get an exception that "'windows-1252' is not a supported encoding name."
Upon running the above code, I get the following exception at the line using (var sqlDataReader = cmd.ExecuteReader()).:
/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding()
at MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[] columns)
at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlDataReader.Close()
at MySql.Data.MySqlClient.MySqlCommand.ResetReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at musense.Startup.ConfigureServices(IServiceCollection services) in /home/memonick/Documents/Projects/musense/Startup.cs:line 46
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at musense.Program.BuildWebHost(String[] args) in /home/memonick/Documents/Projects/musense/Program.cs:line 21
at musense.Program.Main(String[] args) in /home/memonick/Documents/Projects/musense/Program.cs:line 17
It's worth noting that the connection is being established. Thus, for example, I can update a row, or execute SELECT VERSION(). The problem happens only when I try to execute a SELECT statement on a table from ASP.NET Core. The statement itself seems valid, as it executes normally in phpmyadmin.
The test table has the following schema:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL,
`value` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
I've tried various collations, but the exception persists. Any help is welcome!
Upvotes: 0
Views: 588
Reputation: 18265
Your issue rises by the fact that you are using a NuGet package not compatible with .NETCoreApp2.0
:
/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
You need to upgrade MySql.Data 6.9.9
and use at least 6.10.3-rc
version (remember to check show prerelease packages in nuget browser), which is compatible with .netstandard1.3
and above (including .NETCoreApp2.0
).
Upvotes: 1