Read data from a column through C#

Asked

Viewed 881 times

-2

I have a postcode and divided into 2 variables: the first, where the first 4 numbers appear and the second 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. A PC4, where the first 4 numbers appear, the PC3 where the last 3 and the Desc, where the corresponding description appears, that is, where these postal codes are from.

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

  • But at what point do you want to do it? Num query right in the bank? Run a query by its C# code using some ORM or the SqlCommand? Please be more specific.

  • I want to do this in code c#

  • It’s something I’ve never done, I’ve researched and I can’t figure out how to do

  • 1

    Okay, it’s okay that you don’t know, but at least try a little bit to explain to us how do you want to do that.

  • I tried to explain as best I could. I have 2 variables, variable1 has the first 4 numbers of the postal code, variable2 has the last 3. I need to compare variable1 with column PC4 and variable2 with PC3, when variable1 = PC4 and variable2 = PC3, I want to receive)

  • Let’s try again. Do you know how to connect to the database? Your project already connects to the database?

  • Yes, I have everything connected, I have already entered values in the database through the c# and everything.

  • Okay, we got to the part I wanted. How do you input (and read) database data?

  • through INSERT and SELECT

  • Okay, okay, let’s try it again. What you do select and Insert I know, after all there’s no other natural way to do it. Now, what I want to know is: what code you use to make a select in the database?

  • 1

    @Dc post the code, read this and rephrase the question https://answall.com/help/mcve, see that it is an official Help recommendation of the site and not mine. So you avoid your question being closed

  • @jbueno the problem is this ! do not know how to formulate my code to make a select !

  • @DC You already consult some data in the bank. Do not consult?

  • @jbueno The only thing I did in this project was to enter data in the database

  • And what code do you use to "enter data into the database"?

  • Conn. Open(); Com.Commandtext = "INSERT INTO Table(column1, column2, ...) VALUES (value1, value2, ...)"; Com.Executenonquery(); Conn.Close();

Show 11 more comments

1 answer

2


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

Within select you can implement a WHERE clause where you will return only records that satisfy that condition. The comparison return I added in a variable, you can add in a list or do some operation if necessary.

In the comparison part I passed a type Int32 without knowing what is its value in fact, then you need to adapt 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
    }
}
  • That’s exactly what I needed! I’ve adjusted the code the way I needed it, thank you very much !

  • @DC, glad you could help. Hug

  • It helped a lot ! Hug

  • by the way, I have another question, maybe you can help me

  • @DC, say so. What’s your question ?

  • can you see my other questions ? I asked a new question. There’s my question

Show 1 more comment

Browser other questions tagged

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