C# .NET - sqlcommand with variable ifrom C# - Asked By a a on 22-Apr-07 09:24 AM

Hi,

 i want to write the following sqlcommand :

my_field = my_field + 1;

SqlCommand cmd = new SqlCommand("select count(*) from constraints_table where my_field = my_field " , conn);

the cmdline works good if not in a loop (and in the first loop)

but when i use this cmd line in a foreach loop, the value of my_field doesnt change even though i see the variable my_field changes, what do i have to change in the cmdline so the variable my_field will change in every loop?

Thanks TAL

 

Yes, the variable my_field changes, but - Peter Bromberg replied to a a on 22-Apr-07 07:17 PM

that's not what you are passing in your SqlCommand - you are passing the LITERAL TEXT "my_field".   Change it to this:


SqlCommand cmd = new SqlCommand("select count(*) from constraints_table where my_field = "+ my_field.ToString()  , conn);