1

SqlDataReader.HasRows空の結果でも常に true を返す現象に関する多数のトピックを参照した後(特に集計を使用した SQL クエリに関する場合)、コードを完全に乾かします。

ただし、私の例は非常に単純で、phpMyAdmin 側の行がない場合でも,をHasRows返します。TrueFieldCount1

query = "SELECT FK_BarId FROM tlink_bar_beer WHERE FK_BeerId = " + sqlDataReader.GetInt32(0);

MySqlConnection sqlConnexionList = new MySqlConnection("server=localhost;database=beerchecking;uid=root;password=;");
MySqlCommand commandList = new MySqlCommand(query, sqlConnexionList);
sqlConnexionList.Open();

int[] BarsIds;
using (MySqlDataReader sqlDataReaderList = commandList.ExecuteReader())
{
    if (sqlDataReaderList.HasRows)
    {
        try
        {
            BarsIds = new int[sqlDataReaderList.FieldCount];
            int counter = 0;

            if (sqlDataReaderList.Read())
            {
                while (sqlDataReaderList.Read())
                {
                    int id = sqlDataReaderList.GetInt32(counter);
                    BarsIds[counter] = id;
                    counter++;
                }
            }
        }
        finally
        {
            sqlDataReaderList.Close();
        }
    }
    else
    {
        BarsIds = new int[0];
    }
}

sqlConnexionList.Close();

phpMyAdminの結果のように行がない場合にHasRowsをfalseにする方法を知っていますか?

読んでくれてありがとう。

4

1 に答える 1