使用 MySqlDataReader 读取 [英] Reading with MySqlDataReader
问题描述
您好,我在使用 MySqlDataReader
阅读时遇到问题.我尝试将 while()
更改为 if()
并且它起作用了.所以我在 while (Reader.Read())
上做错了.谢谢你的回答.(今天的另一个问题已解决,评论的人帮助了我xd)
Hello I'm having problem to read with MySqlDataReader
. I've tried to change while()
to if()
and it worked then. So I'm doing something wrong with while (Reader.Read())
. Thanks for answer. ( The other question today is fixed, someone that commented helped me xd )
using (MySqlCommand cmd = new MySqlCommand
("SELECT * FROM `citationer`", mysqlCon))
{
try
{
MySqlDataReader Reader = cmd.ExecuteReader();
while (Reader.Read()) // this part is wrong somehow
{
citationstexter.Add(Reader.GetString(loopReading)); // this works
loopReading++; // this works
}
Reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
推荐答案
您的问题是将 loopReading
参数用于 GetString
.此参数应该是从零开始的列序号(列号),但您要为读取的每一行增加它.
Your problem is the use of the loopReading
parameter to GetString
.
This parameter should be the zero-based column ordinal (the column number), but you're incrementing it for every row you read.
请参阅此处了解更多信息:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring.aspx
See here for more info: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring.aspx
您应该将 0 用于引文表的第一列,1 用于第二列,以此类推.
You should use 0 for the first column of your citationer table, 1 for the second column, etc.
另外,使用
using(MySqlDataReader Reader = cmd.ExecuteReader())
{
...
}
就像您为 mySqlCommand 对象所做的那样,以防止内存泄漏(但这不是您的问题.)
just like you did for the mySqlCommand object to save a memory leak (but this is not your problem.)
这篇关于使用 MySqlDataReader 读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!