[英]SQLiteDataReader.read() empties results?
I have a method in my C# class that calls a single result from an SQLite database. 我在C#类中有一个方法,它从SQLite数据库中调用一个结果。 My database connection opens successfully, but after running breakpoints at different places, I noticed something odd. 我的数据库连接打开成功,但在不同的地方运行断点后,我注意到一些奇怪的事情。 This is my code: 这是我的代码:
Dictionary<string, string> getResult(string id)
{
dataConn.Open();
SQLiteCommand comm = dataConn.CreateCommand();
comm.CommandText = "SELECT * FROM [tableName] WHERE id='" + id + "'";
SQLiteDataReader result = comm.ExecuteReader();
Dictionary<string, string> resultDict = new Dictionary<string, string>();
/*
This doesn't work.
while(result.Read())
{
resultDict.Add(result.GetName(0), result.GetString(0));
}
*/
result.Read();
for (int i = 0; i < result.FieldCount; i++)
{
resultDict.Add(result.GetName(i), result.GetString(i));
}
result.Dispose();
comm.Dispose();
dataConn.Close();
return resultDict;
}
I know that I can use SQLiteParameters
instead of inline concatenation, but for some reason they were breaking my query. 我知道我可以使用SQLiteParameters
而不是内联串联,但由于某种原因,他们破坏了我的查询。
In my first loop, if I place a breakpoint just before it starts, I can see that my query has returned a single result, exactly what I need. 在我的第一个循环中,如果我在它开始之前放置一个断点,我可以看到我的查询返回了一个结果,正是我需要的。 But, when I go into my loop, before I execute any code other than reader.Read()
, VS2010 says "Enumeration yielded no results." 但是,当我进入循环时,在执行除reader.Read()
之外的任何代码之前,VS2010说“枚举没有产生任何结果”。 but they were there moments before? 但他们之前就在那里?
So, I tried the second loop. 所以,我尝试了第二个循环。 Executing reader.Read()
, and then looping through returned fields. 执行reader.Read()
,然后循环返回返回的字段。 When I do that, VS2010 throws the error "No current row selected.". 当我这样做时,VS2010会抛出错误“No current row selected。”。
I'm completely lost with this, I have no idea what's wrong, because the rest of my SQLite connections for other database files work fine. 我完全迷失了,我不知道出了什么问题,因为我的其他SQLite连接对其他数据库文件工作正常。 I've tested my query in SQLite Administrator
, and my query executes perfectly fine. 我在SQLite Administrator
测试了我的查询,我的查询执行得非常好。 All I'm aiming for is to end up with a dictionary where the key is the name of the field, and the value is the result of said field. 我的目标是最终得到一个字典,其中键是字段的名称,值是所述字段的结果。 Any ideas on what's wrong? 关于什么是错的任何想法? Or, is there a way I can make this code simpler, in the process of fixing it? 或者,有没有办法在修复它的过程中使这段代码变得更简单?
Solved. 解决了。 For some reason, using .GetString(0)
or .GetString(i)
was breaking it. 出于某种原因,使用.GetString(0)
或.GetString(i)
打破了它。 However, when I turned it into .GetValue(0).ToString()
it worked fine. 但是,当我把它变成.GetValue(0).ToString()
它工作正常。 I have no idea why this is the case, it's beyond me, but it works. 我不知道为什么会这样,它超出了我的范围,但它确实有效。
Its because GetValue(0)
didn't return a string. 因为GetValue(0)
没有返回字符串。
Casting it to a string allowed you to add it to the Dictionary<string,string>
. 将其转换为字符串允许您将其添加到Dictionary<string,string>
。
I had a similar problem, in my case the table was empty, and i was asking for MAX() of some collumn. 我有一个类似的问题,在我的情况下,表是空的,我要求一些collumn的MAX()。 I solved it by first checking that COUNT(*) is not 0. 我通过首先检查COUNT(*)不是0来解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.