Compare variables with values from a SQL Server table C #

0

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

Forexample:Ifmyvariableis23.00,Iwanttoreturnthe6!I'vetriedthiscodebutitdoesnotwork:

conn.Open();SqlCommandcommand=newSqlCommand("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 it?

    
asked by anonymous 26.04.2017 / 19:19

1 answer

2

I suggest that you run the search directly on the database server to prevent all contents of the VAT table from traveling over the network.

SELECT Code from IVA where Value = @taxpercentage

In this way, only rows that meet the requirement will be routed over the network. And search processing will occur on the database server.

    
26.04.2017 / 19:49