Using SqlParameter to create Order By clause

前端 未结 5 2225
一向
一向 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 07:10

    I found an example how to do this here

    you can define different sort orders in a CASE-structure and execute them appropriately to your variable value:

      SELECT CompanyName,
             ContactName,
             ContactTitle
    
        FROM Customers
    
    ORDER BY CASE WHEN @SortOrder = 1 THEN CompanyName
                  WHEN @SortOrder = 2 THEN ContactName
             ELSE ContactTitle
    

    I didn't test it myself but it could work. You can give it a try. An obvious disadvantage is that you have to code all the order-by statements.

提交回复
热议问题