0

我正在尝试使用以下代码从 db 中选择数据:

 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);

但它不起作用:

Incorrect syntax near '@Count'.

为什么它不起作用?

4

2 回答 2

2

请试试这个SELECT TOP (@Count) * FROM Forecasts Order by [ForecastId] DESC

请注意,@count 用括号括起来。

于 2012-07-20T11:56:17.493 回答
0

该语法看起来像 Microsoft Access 数据库。如果是,则不支持 TOP 参数。您将不得不构建字符串。

@"SELECT TOP " + Count + " * FROM Forecasts Order by [ForecastId] DESC";
于 2012-07-20T11:29:06.640 回答