简体   繁体   English

reader.Read()仅读取一次,即使有多行要读取

[英]reader.Read() only read once even when there are multiple rows to read

I have the code 我有代码

while (reader.Read())
{
    if (reader[incrementer]!=DBNull.Value){
        string playerToInform = reader.GetString(incrementer).ToString();
        string informClientMessage = "ULG=" + clientIP + ","; //User Left Game
        byte[] informClientsMessage = new byte[informClientMessage.Length];
        informClientsMessage = Encoding.ASCII.GetBytes(informClientMessage);
        playerEndPoint = new IPEndPoint(IPAddress.Parse(playerToInform), 8001);
        clientSocket.SendTo(informClientsMessage, playerEndPoint);
    }
    incrementer++;
}

which after debugging my code i see contains 4 entries. 调试我的代码后,我看到其中包含4个条目。 However only the first result is ever read from the reader. 但是,只有第一个结果才能从阅读器中读取。 After the first iteration to find if the result returned is null or not the loop starts again and immediately finishes even though there are three more rows to read. 在第一次迭代中查找返回的结果是否为null之后,即使还有三行要读取,循环也会再次开始并立即结束。

Any ideas as to why this may be occuring would be apprechiated. 为什么会发生这种情况的任何想法都将得到赞赏。

edit - this is the reader i used 编辑-这是我使用的读者

OleDbDataReader reader = dBConn.DataSelect("SELECT player1_IP, player2_IP, player3_IP, player4_IP FROM running_games WHERE game_name = '" + gameName + "'", updateGameList);

The indexer of DbDataReader ( DataReader is something else) or a database specific subclass, returns the value of the specified (by index or name ). DbDataReader的索引器( DataReader除外)或数据库特定的子类,返回指定值(按index或name )。

While DbDataReader.Read() moves to the next row. DbDataReader.Read()移至下一行。

If you want to apply the same logic to multiple columns you need to loop over the columns, and the rows: 如果要对多个列应用相同的逻辑,则需要遍历各列各行:

while (db.Read()) {

  for (var colIdx = 0; colIdx < columnCount. ++colIdx) {
    if (!db.IsDbNll(colIdx)) {
      string value = db.GetString(colIdx);
      // Process value
    }
  }

}

Incrementer is unnecessary. 增量器是不必要的。 reader.Read() advances to next record and returns false if there are no more rows. reader.Read()前进到下reader.Read()记录,如果没有更多行,则返回false

Check documentation on msdn 查看有关MSDN的文档

You're incrementing "incrementer" as if that was the row number, but a DataReader holds only one row per Read() and the indexing is for the field number. 您正在递增“ incrementer”,就像那是行号一样,但是DataReader每个Read()仅保留一行,而索引是针对字段号的。

Use this: 用这个:

while (reader.Read())
{
    for(int colNum = 0; colNum < 4; colNum++)
    {
        if (reader[colNum]!=DBNull.Value)
        {
            string playerToInform = reader.GetString(colNum).ToString();
            string informClientMessage = "ULG=" + clientIP + ","; //User Left Game
            byte[] informClientsMessage = new byte[informClientMessage.Length];
            informClientsMessage = Encoding.ASCII.GetBytes(informClientMessage);
            playerEndPoint = new IPEndPoint(IPAddress.Parse(playerToInform), 8001);
            clientSocket.SendTo(informClientsMessage, playerEndPoint);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM