How to make second select in "reader.Read"?

0

Here is the code:

public FileContentResult Foto()
        {
            byte[] byte_image = null;
            string query = "SELECT * FROM Tabela WHERE Coluna = @Parameter";
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            using (var command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@Parameter", User.Identity.Name);
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        if (!Convert.IsDBNull(reader["Byte_Image"]))
                        {
                            byte_image = (byte[])reader["Byte_Image"];
                        }
                        else
                        {
                            //Segundo select aqui
                            string query = "SELECT * FROM Tabela1 WHERE Coluna1 = @Parameter";
                        }
                    }
                }
            }
            return new FileContentResult(byte_image, "image/jpeg");
        }

At line else how to do second select with reader?

    
asked by anonymous 27.12.2016 / 20:40

1 answer

0

In this case, there is no need to use the same object IDataReader , so you can create a new object IDataReader to perform this operation, within the scope of else .

Your code looks like this:

public FileContentResult Foto()
{
    byte[] byte_image = null;
    string query = "SELECT * FROM Tabela WHERE Coluna = @Parameter";
    using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    using (var command = new SqlCommand(query, connection))
    {
        command.Parameters.AddWithValue("@Parameter", User.Identity.Name);
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                if (!Convert.IsDBNull(reader["Byte_Image"]))
                {
                    byte_image = (byte[])reader["Byte_Image"];
                }
                else
                {
                    //Segundo select aqui
                    string query = "SELECT * FROM Tabela1 WHERE Coluna1 = @Parameter";

                    using (var command2 = new SqlCommand(query, connection))
                    {
                      command2.Parameters.AddWithValue("@Parameter", User.Identity.Name);

                      using (var reader2 = command2.ExecuteReader()) {

                        //OPERAÇÃO SOBRE O reader2

                      }
                    }
                }
            }
        }
    }
    return new FileContentResult(byte_image, "image/jpeg");
}
    
27.12.2016 / 21:35