Read data from a column through C # [closed]

-2

I have a postal code and divided it into 2 variables: the first, where the first 4 numbers appear and the second one where the last 3 appear.

Ex:

  

4300-234 - first = 4300 ; second = 234 .

I have a table in the database that has 3 columns referring to postal codes. The PC4 , where the first 4 numbers appear, the PC3 where the last 3 appear and the Desc , where the corresponding descriptive appears, that is, where are these postal codes.

What I need is to compare my first variable with the PC4 column, compare the second variable with the PC3 column, and then when my variables are equal to the columns PC4 and PC3 , I want receive the corresponding data from column DESC .

    
asked by anonymous 18.04.2017 / 17:47

1 answer

2

This is an idea of how you can do it, there are n ways to do it, I believe you can return some data to go testing.

Within the select you can implement a WHERE clause where you will only return records that satisfy that condition. Returning the comparison I've added in a variable, you can either add it to a list or make some operation if needed.

In the comparison part I passed an Int32 type without knowing what its actual value is, then you need to tailor the code to your need.

public void SelectDescricao()
{
    try
    {
        using(SqlConnection conn = new SqlConnection("Sua string de conexão"))
        {
             using (SqlCommand cmd = new SqlCommand("SELECT coluna1, coluna2, coluna3_Descricao FROM Tabela", conn))
             {
                cmd.CommandType = CommandType.CommandText;

                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            if( variavel1 == Convert.ToInt32(rd["coluna1"].ToString()) && variavel2  == Convert.ToInt32(rd["coluna2"].ToString()))
                            {
                                varDescricao = Convert.ToInt32(rd["coluna3_Descricao"].ToString());                                        
                            }
                        }
                    }
                }
            }  
        }                         
    }
    catch (SqlException ex)
    {
        // tratamento da exceção
    }
}
    
19.04.2017 / 01:14