reader.Read()仅读取一次,即使有多行要读取 [英] reader.Read() only read once even when there are multiple rows to read

查看:146
本文介绍了reader.Read()仅读取一次,即使有多行要读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码

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++;
}

在调试我的代码后,我看到其中包含4个条目.但是,只有第一个结果才能从阅读器中读取.在第一次迭代中查找返回的结果是否为null之后,即使还要读取三行,循环也会再次开始并立即结束.

which after debugging my code i see contains 4 entries. 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.

关于为什么会发生这种情况的任何想法都会被赞赏.

Any ideas as to why this may be occuring would be apprechiated.

编辑-这是我使用的读者

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

推荐答案

您正在递增"incrementer",就像那是行号一样,但是DataReader每个Read()仅保留一行,并且索引适用于该字段数字.

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.

使用此:

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);
        }
    }
}

这篇关于reader.Read()仅读取一次,即使有多行要读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆