[英]OracleDataReader reader.Read() starts to read rows from the second record row, skipping the first row record
I have a SQL command and it returns 8 rows from the database.我有一个 SQL 命令,它从数据库返回 8 行。
When I use reader.Read() it starts at the second line, so I loose the fist row result.当我使用 reader.Read() 时,它从第二行开始,所以我松开了第一行结果。 I don't know why it's skipping the first row record.我不知道为什么它会跳过第一行记录。
command.CommandText = "SELECT ATTRIBUTE_DESCRIPTION, SUBSTR(ATTRIBUTE_DATATYPE, 2, 6) FROM " + proj.PID_Schema + "PIDD.ATTRIBUTES@" + proj.PID_Database + " WHERE " +
"attribute_name LIKE 'Controller' " +
"OR attribute_name LIKE 'Initials' " +
"OR attribute_name LIKE 'IOType' " +
"OR attribute_name LIKE 'NetworkType' " +
"OR attribute_name LIKE 'SignalOutput' " +
"OR attribute_name LIKE 'SignalInput' " +
"OR attribute_name LIKE 'SPIInstrumentType' " +
"OR attribute_name LIKE 'Substation' ORDER BY 1";
try
{
reader = command.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
while (reader.Read())
{
codelistsNumbersDict.Add(reader.GetValue(0).ToString(), reader.GetValue(1).ToString());
}
}
}
catch (Exception)
{
}
connection.Close();
return codelistsNumbersDict;
}
Do you have two reads:你有两个读物:
reader = command.ExecuteReader();
reader.Read(); // <--- FIRST READ (skipe first row)
if (reader.HasRows)
{
while (reader.Read()) // <---- SECOND READ
{
Because of this, you starting reading on second row.因此,您开始阅读第二行。
If you take a look to Retrieve data using a DataReader sample, it only has one read:如果您查看使用 DataReader 示例检索数据,它只有一次读取:
SqlDataReader reader = command.ExecuteReader();
// <-- No read here on sample!
if (reader.HasRows)
{
while (reader.Read()) // <-- Just ONE READ
{
Doc screenshot:文档截图:
As mentioned in @dani-herrera's answer, you are calling reader.Read()
twice.正如@dani-herrera 的回答中所提到的,您正在调用reader.Read()
两次。
Also, you don't need the if (reader.HasRows)
, since if there are no rows the code inside the do loop won't execute.此外,您不需要if (reader.HasRows)
,因为如果没有行,则 do 循环内的代码将不会执行。 If you need to know the rows processed, you can always add a counter.如果您需要知道处理的行,您可以随时添加一个计数器。
try
{
reader = command.ExecuteReader();
while (reader.Read())
{
codelistsNumbersDict.Add(reader.GetValue(0).ToString(), reader.GetValue(1).ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.