如何使用參數佔位符將列名稱值作爲SQL參數傳遞?如何使用佔位符作爲SQL參數傳遞列名值
的目標是有這個工作:
var sql = "SELECT * FROM Condos WHERE @0 LIKE @1";
var sqlData = db.Query(sql,choice,"%"+searchString+"%");
choice
是將存儲column name
@0
是column name
(我沒有成功,以它作爲參數傳遞)
@1
是一個(我沒有問題)
已經閱讀並嘗試了很多東西:
下面一個不拋出任何錯誤,但不會帶來任何數據
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+"%");
be低一個拋出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 '"[email protected]+"' 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 ['"[email protected]+"'] LIKE @0";
var sqlData = db.Query(sql,"%"+searchString+"%");
幫助!!!!!!!!!!!!!!
你使用的是什麼rdbms? SQL服務器,MySQL,甲骨文? – Taryn
SQL Server 2008 Express在WebMatrix – user1455103