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.
我正在尝试使用ASP.NET Core连接到我的本地数据库 - 我可以使用localhost / phpmyadmin访问它。我是这门语言的新手,我正在努力解决与MySQL相关的异常问题。
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()).
使用(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."
我在appsettings.json中设置连接字符串。没有charset,我得到一个例外,“'windows-1252'不是受支持的编码名称。”
Upon running the above code, I get the following exception at the line using (var sqlDataReader = cmd.ExecuteReader()).:
运行上面的代码后,我在使用(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.
值得注意的是,正在建立连接。因此,例如,我可以更新一行,或执行SELECT VERSION()。只有当我尝试从ASP.NET Core对表执行SELECT语句时才会出现此问题。语句本身似乎有效,因为它在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!
我尝试了各种排序规则,但例外情况仍然存在。欢迎任何帮助!
1 个解决方案
#1
1
Your issue rises by the fact that you are using a NuGet package not compatible with .NETCoreApp2.0
:
您使用与.NETCoreApp2.0不兼容的NuGet包时,问题就出现了:
/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
).
您需要升级MySql.Data 6.9.9并使用至少6.10.3-rc版本(记得检查nuget浏览器中的show prerelease软件包),它与.netstandard1.3及更高版本(包括.NETCoreApp2.0)兼容。
#1
1
Your issue rises by the fact that you are using a NuGet package not compatible with .NETCoreApp2.0
:
您使用与.NETCoreApp2.0不兼容的NuGet包时,问题就出现了:
/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
).
您需要升级MySql.Data 6.9.9并使用至少6.10.3-rc版本(记得检查nuget浏览器中的show prerelease软件包),它与.netstandard1.3及更高版本(包括.NETCoreApp2.0)兼容。