简体   繁体   中英

Invalid attempt to read data when no data is present when getting all values from a row in a SQL Server table

I have a method to return the values for all columns given a SQL query and I see that I get Invalid attempt to read data when no data is present on the line var fieldCount = reader.GetValues(values) , however, when I look at values it has 88 items.

internal List<string> GetColumnValuesForRow(SqlDataReader reader)
{
    var listOfItems = new List<string>();
    // Given a SqlDataReader, use the GetValues 
    // method to retrieve a full row of data. 
    // Test the GetValues method, passing in an array large 
    // enough for all the columns.
    var values = new Object[reader.FieldCount];
    var fieldCount = reader.GetValues(values);

    Console.WriteLine("Retrieved {0} columns.",
    fieldCount);
    for (var i = 0; i < fieldCount; i++)
    {
        Console.WriteLine(values[i]);
        listOfItems.Add(values[i].ToString());
    }

    return listOfItems;
}

I am calling the method above from this method....

internal List<string> SqlQueryForRow(string query)
{
    var conn = new SqlConnection(ConnectionString);
    var cmd = new SqlCommand(query, conn);
    SqlDataReader reader;

    cmd.CommandText = query;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;

    conn.Open();

    reader = cmd.ExecuteReader();
    // Data is accessible through the DataReader object here.
    // TODO add handling for more than one row; i.e. just take the first row
    var rowOfItems = GetColumnValuesForRow(reader);

    conn.Close();

    return rowOfItems;
}

Why is it that when I get the values it shows 88 items but then is throwing Invalid attempt to read data when no data is present when I call reader.GetValues(values) ?

I tested the query is SQL Server Management Studio and it does only return one record.

The following code is working fine for me when accessing and reading row(s) from a SQL database. As DJ KRAZE mentioned, always check if you have any rows first.

if (reader.HasRows)
{
     while (reader.Read())
     {
          // Read all column data from row here, one row at a time
     }
}

This worked....

    internal List<string> SqlQueryForRow(string query)
    {
        var conn = new SqlConnection(ConnectionString);
        var cmd = new SqlCommand(query, conn)
        {
            CommandText = query,
            CommandType = CommandType.Text,
            Connection = conn
        };

        conn.Open();

        var reader = cmd.ExecuteReader();
        // Data is accessible through the DataReader object here.
        // TODO add handling for more than one row; i.e. just take the first row
        var rowOfItems = new List<string>();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                rowOfItems = GetColumnValuesForRow(reader);
            }
        }

        conn.Close();

        return rowOfItems;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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