如何使用参数占位符将列名值作为 SQL 参数传递?
目标是让这个工作:
var sql = "SELECT * FROM Condos WHERE @0 LIKE @1";
var sqlData = db.Query(sql,choice,"%"+searchString+"%");
choice
是一个变量,将存储column name
@0
是一个column name
(我没有成功将它作为参数传递)
@1
是一个search string
(我没有问题)
阅读并尝试了很多东西:
下面一个不会抛出任何错误但不会带来任何数据
var sql = "SELECT * FROM Condos WHERE @0 LIKE @1";
var sqlData = db.Query(sql,choice,"%"+searchString+"%");
低于一投Column name not valid = '@0'
var sql = "SELECT * FROM Condos WHERE [@0] LIKE @1"
var sqlData = db.Query(sql,choice,"%"+searchString+"%");
低于一投Column name not valid = 'choice'
var sql "SELECT * FROM Condos WHERE choice LIKE @0");
var sqlData = db.Query(sql,"%"+searchString+"%");
低于一投Must declare scalar variable "@choice"
var sql "SELECT * FROM Condos WHERE @choice LIKE @0");
var sqlData = db.Query(sql,"%"+searchString+"%");
下面一个不会抛出任何错误但不会带来任何数据
var sql = "SELECT * FROM Condos WHERE '@choice' LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
下面一个不会抛出任何错误但不会带来任何数据
var sql = "SELECT * FROM Condos WHERE '@choice' LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
下面一个不会抛出任何错误但不会带来任何数据
var sql = "SELECT * FROM Condos WHERE '"+choice+"' LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
下一个:BIG CRASH
var sql = "SELECT * FROM Condos WHERE '"+@choice+"' LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
低于一投Column name not valid = 'NameShort'
这正是正确的列名
var sql = "SELECT * FROM Condos WHERE ['"+choice+"'] LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
下一个:BIG CRASH
var sql = "SELECT * FROM Condos WHERE ['"+@choice+"'] LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
帮助 !!!!!!!!!!!!!!