Using SqlParameter to create Order By clause

前端 未结 5 2261
一向
一向 2020-12-31 06:52

I am trying to move all of my references to variables in SQL statements to the SqlParameter class however for some reason this query fails.

string orderBy =          


        
5条回答
  •  太阳男子
    2020-12-31 06:59

    You're just concatenating strings. A simpler approach would be:

    string orderBy = "name ASC";
    string selectCommand = "SELECT cat_id AS id, cat_name AS name FROM table_name ORDER BY " + orderBy;
    

    I'm assuming you're doing this at all because you're letting the caller decide sort field/direction, hence orderBy separated.

    Parameters, as the error message sort of obliquely hints at, would be used in a WHERE clause, e.g. WHERE someColumn = @someValue etc.

提交回复
热议问题