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?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 18, 2012, 10:35 PM
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.
SenthilkumarPosted Apr 19, 2012, 11:37 PM
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.
Sam HobbsPosted Apr 19, 2012, 9:41 PM
Camila PiaiPosted Apr 19, 2012, 7:40 PM