Search code examples
c#.netsql-serverparameterscode-behind

Changing TOP rows parameter from code behind


I know we can add parameter for query Select * from abc where xyz=@xyz using SqlCommand.Parameters.AddWithValue("@xyz",xyz)from C# code. But is it possible to change top rows paramenter for query like Select TOP @rows * from abc where xyz=@xyz like SqlCommand.Parameters.AddWithValue("@rows",32)?

Because whenever I tried it, it showed me this exception: Incorrect syntax near '@rows'.


Solution

  • You can defintely pass a integer into the top clause. You should be able to do something like this.

    select top (@Rows) *
    from dbo.MyTable
    order by something