DataReader is null or empty

前端 未结 2 537
情歌与酒
情歌与酒 2021-01-16 09:11

Using C#

I have a datareader that return a lsit of records from a mysql database.

I am trying to write code that checks if the datareader isnull. The logic

2条回答
  •  旧巷少年郎
    2021-01-16 09:37

    Use dr1.Read() to check that there is a row before attempting to read values. Read gets the first row initially, and then returns subsequent rows, returning true if row available or empty/end of set.

    eg.

    // for reading one row
    if (rd1.Read())
    {
        // do something with first row
    }
    
    // for reading thru multiple rows
    while (rd1.Read())
    {
        // do something with current row
    }
    

提交回复
热议问题