Compare variables with values from an SQL Server table C#

Asked

Viewed 958 times

0

I have 1 table with 2 columns (Code and Value). I’m writing a code in C# and I have a variable taxpercentage. What I need is to return the column value Code, when the variable taxpercentage is equal to the value of the column Value. I have to read the column Value and when I find a match, I return the Code.

inserir a descrição da imagem aqui

For example: If my variable is 23.00, I want to return the 6 ! I tried this code, but it doesn’t work:

conn.Open();
SqlCommand command = new SqlCommand("SELECT Value, Code FROM IVA", conn);
SqlDataReader rd = command.ExecuteReader();

if(rd.HasRows)
{
   while (rd.Read())
   {
      if (taxpercentage == rd["Value"].ToString())
      {
         codeiva = rd["Code"].ToString();
         Console.WriteLine(codeiva);
      }
   }
}
conn.Close();

Does anyone know how I can do ?

  • How you are assigning value in variable taxpercentage?

  • Sets the variable declaration taxpercentage

  • The taxpercentage variable is a value I’m reading from an xml file, so: Xmlnodelist taxlist = doc.Getelementsbytagname("Tax"); foreach (Xmlnode xn1 in taxlist) { string taxpercentage = xn1["Taxpercentage"]. Innertext; }

1 answer

2

I suggest you run the search directly on the database server, to prevent all the contents of the IVA table from moving over the network.

SELECT Code from IVA where Value = @taxpercentage

This way only the lines that meet the requirement will travel through the network. And the search processing will take place in the database server.

  • and then how can I assign the value I receive to a variable ? I receive the value of the respective Code and want to put this value in a variable called "codex"

Browser other questions tagged

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