2012-07-20 74 views
0

我想使用此代码来选择DB数据:添加参数命令ADO.NET

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'. 

为什么不是工作?

+1

您应该在您正在使用的数据库上添加备注。 – Fionnuala 2012-07-20 11:29:50

回答

2

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

请注意,@count被括号包围。

+0

另外,请在这里http://sqlserver2000.databases.aspfaq.com/how-do-i-use-a-variable-in-a-top-clause-in-sql-server找到一些关于TOP子句的附加信息。 html – sergeyG 2012-07-20 11:58:30

+0

这是工作!非常感谢。 – 2012-07-20 12:05:03

0

该语法看起来像一个Microsoft Access数据库。如果是,它不支持TOP的参数。你将不得不建立字符串。

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