Ilya Shpakovsky
Ilya Shpakovsky

Reputation: 291

Add parameter to command ADO.NET

I'm trying to select data from db with using this code:

 getForecastsCommand.CommandText = @"SELECT TOP @Count * FROM Forecasts Order by [ForecastId] DESC";

   var countParam = getForecastsCommand.CreateParameter();
   countParam.ParameterName = "@Count";
   countParam.Value = count;
   countParam.DbType = DbType.Int32;
   getForecastsCommand.Parameters.Add(countParam);

But it isn't work:

Incorrect syntax near '@Count'.

Why isn't it work?

Upvotes: 0

Views: 1388

Answers (2)

sergeyG
sergeyG

Reputation: 54

Please try this SELECT TOP (@Count) * FROM Forecasts Order by [ForecastId] DESC

Please, note that @count is surrounded by parenthesis.

Upvotes: 2

Fionnuala
Fionnuala

Reputation: 91376

That syntax looks like a Microsoft Access database. If it is, it does not support parameters for TOP. You will have to build the string.

@"SELECT TOP " + Count + " * FROM Forecasts Order by [ForecastId] DESC";

Upvotes: 0

Related Questions