如何通过列名值使用参数占位符的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+"%");
救命 !!!!!!!!!!!!!!