我在VS2015中使用C#和MySQl来查询我的数据库。但是,当我输入的列名不是Int或Datetime类型时,datareader会返回错误。
但是,数据库中存在一个列,该列与我输入的字符串作为数据库名称相匹配。我的代码:
string DateQuery = "SELECT timeStamp, amount, tax, name FROM log.transactions";
MySqlCommand cmd3 = new MySqlCommand(DateQuery, connectionString);
MySqlDataReader dataReader3 = cmd3.ExecuteReader();
在编译时,这将返回错误:“mscorlib.dll中发生了'System.Collections.Generic.KeyNotFoundException'类型的未处理异常
附加信息:字典中没有给定的密钥。“
在dataReader3上。
但是如果从查询中删除名称(VarChar类型)列,它将使用timeStamp(DateTime)amount(Int)和tax(Int)编译并执行。
它指的是什么字典不包含密钥?为什么它只影响不是Int或DateTime的类型?
编辑:完整代码发布在下面
using System;
using MySql.Data.MySqlClient;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Program generateReport = new Program();
generateReport.Kickoff();
}
public void Kickoff()
{
string dbString = "server=XXXXXX;uid=XXXXXX;" + "pwd=XXXXX;database=XXXXX;";
MySqlConnection connectionString = new MySqlConnection(dbString);
connectionString.Open();
Console.WriteLine("\nAttempting to connect to database...");
if (connectionString.State.ToString() == "Open")
{
Console.WriteLine("Connection successful.");
}
else
{
Console.WriteLine("Connection failed. :(");
return;
}
string DateQuery = "SELECT timeStamp, amount, tax, name FROM log.transactions";
MySqlCommand cmd3 = new MySqlCommand(DateQuery, connectionString);
MySqlDataReader dataReader3 = cmd3.ExecuteReader();
}
}
}
编辑:Stack Trace我相信:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion version String CharSetName)
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41()
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData()
at MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at TestApp.Program.Kickoff() in c:\users\Testuser\documents\visual studio 2015\Projects\TestApp\TestApp\Program.cs:line 42
at TestApp.Program.Main(String[] args) in c:\users\Testuser\documents\visual studio 2015\Projects\TestApp\TestApp\Program.cs:line 13
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:0)
请尝试使用以下名称:
SELECT timeStamp, amount, tax, `name` FROM log.transactions