I'm trying to use a variable in WHERE Statement in C#
Example:
"Select * from table1 WHERE cod_table1 = contr"
(contr is a int variable already declared);
But it's not working.
What can be done?
Variable in WHERE Statement
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.

SenthilkumarRank #387 · 6.5K reputationPosted Apr 18, 2012, 6:35 PM EDT
Usually in sql server the where condition will be applied like this.
If it is int column then
SELECT * FROM table1 WHERE cod_table1 = 10
If it is varchar/char/text then
SELECT * FROM table1 WHERE cod_table1='test'
Similarly when you pass from the front end then
int contr = 10;
string sqlQuery = "SELECT * FROM table1 WHERE code_table1="+contr.ToString();
Hope this will help you to solve the issue.
Camila Piai7 reputationPosted Apr 19, 2012, 3:40 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 19, 2012, 5:41 PM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 7:37 PM EDT
The inline query always not best practice to do. Better go for parameterized or stored procedure.
The inline query will lead into sql injection problem as well as performance issues.