Is there any difference in using the Read() method of the Sqldatareader object?

Asked

Viewed 316 times

4

I am running the code snippet below, however, I was left with doubt related to the method Read() of the object Sqldatareader.

using (SqlDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {
        if (reader.Read())
        {
             pconfigWS.Usuario = reader["usuario"].ToString();
             pconfigWS.Senha = reader["senha"].ToString();
             pconfigWS.EndConWSPorto = reader["endConWSPorto"].ToString();
        }
    }
}

I wonder if there’s a difference in using While(reader.Read()), if(reader.Read()) and reader.Read()

I know that the result of the query will be only one line, however, I am with this doubt.

Does anyone know how to say ?

Thank you

  • From what I understand, man, the difference is that if you use while read, it will pass record by record that you bring from the bank suppose you brought 10 record in a select it goes through one by one, o if read it will read only the first independent of how many Voce brought it if there is record to read, if there is no it goes through if, to use direct without if nor while, it will read the first record only, but if you don’t bring anything and you try to use it will throw you an exception.

  • So Jhonatan, I understood your comment and I already made this test, however, my doubt is if there is a rule of use or if there are good practices for this case understand.

1 answer

2


Every call to Read() will advance the reading of the search results.

In that case, the difference would be:

//contiuna lendo o próixmo até não houver mais
while (reader.Read())
{
  ...
}

//lê o próximo e me fala se for sucedido...
if (reader.Read())
{
  ...
}

//lê o próximo
reader.Read();

I think it’s important to know that if(reader.Read()) will advance to the next record, then the next reader.Read() will be the record next, not the current.

For example, your codex explained:

using (SqlDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows) //verifique se o leitor tem dado algum
    {
        if (reader.Read()) //lê os dados - se tiver mais de um registro, não está sendo lido
        {
             pconfigWS.Usuario = reader["usuario"].ToString();
             pconfigWS.Senha = reader["senha"].ToString();
             pconfigWS.EndConWSPorto = reader["endConWSPorto"].ToString();
        }
    }
}

And an example that can give unexpected results:

using (SqlDataReader reader = cmd.ExecuteReader())
{
    List<PConfigWS> resultados = new List<obj>();
    if (reader.Read())
    {
        while (reader.Read())
        {
             var pconfigWS = new PConfigWS();
             pconfigWS.Usuario = reader["usuario"].ToString();
             pconfigWS.Senha = reader["senha"].ToString();
             pconfigWS.EndConWSPorto = reader["endConWSPorto"].ToString();
             resultados.Add(pconfigWS);
        }
    }
}

In this case, the first record would be ignored (because of the if(reader.Read()), that probably wouldn’t be the desired result. Use reader.HasRows it would be the right way, as you used.

There is not necessarily 'good practice' between these functions - they are literally the same function, being used in different forms. It all depends on whether you are reading just one record or several.

  • Disculpe there Portuguese, is not my native language

  • Thank you for your explanation, my doubt has been removed.

  • @brazilianldsjaguar I changed "records" to "records", I’m not sure about Portugal, but here in Brazil record we use "record" with another meaning - I even tried to consult a staff in the chat, but there’s no one from Portugal there. In any case, it is still possible to reverse the edition if this is common there. Otherwise, very good answer and your English =D

  • @jbueno thanks - actually I was translating the English word "record" - record really explains better. :)

  • Yeah, I figured =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.