'Products' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 'Products' 附近有语法错误。源错误: 
行 402: Para3.Value = CategoryID;
行 403:            
行 404:            SqlDataReader result = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
行 405: return result;
行 406: }
 源文件: c:\Inetpub\wwwroot\Shop\ShopWebSite\App_Code\Classes\Pub.cs    行: 404 
源码:
public SqlDataReader spProductsTop(string Top, string OrderBy, string CategoryID) 
{
ConnOpen();
SqlCommand1 = new SqlCommand("spProductsTop", SqlConnection1);
SqlCommand1.CommandType = CommandType.StoredProcedure; SqlParameter Para1 = new SqlParameter("@Top", SqlDbType.VarChar, 25);
SqlParameter Para2 = new SqlParameter("@OrderBy", SqlDbType.VarChar, 50);
SqlParameter Para3 = new SqlParameter("@CategoryID", SqlDbType.VarChar, 25);            SqlCommand1.Parameters.Add(Para1);
SqlCommand1.Parameters.Add(Para2);
SqlCommand1.Parameters.Add(Para3);
Para1.Value = Top;
Para2.Value = OrderBy;
Para3.Value = CategoryID;
            
            SqlDataReader result = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);此句有问题不能测试通过???
return result;
}存储过程可以通过测试:

解决方案 »

  1.   

    ALTER procedure [dbo].[spProductsTop]
    (@top varchar(25),
     @orderby varchar(50),
     @categoryId varchar(25))
    as
    set nocount on
    declare @ssql varchar(500)
    select @sSql='select top'+@top
    select @sSQL=@sSql+'o.*,s.Name as CategoryName,'
    select @sSQL=@sSql+ 'p.Name as Vendor'
    select @sSQL=@sSql+ 'from Products o inner join 'select @sSQL=@sSql+ 'categories  s on o.CategoryId=s.Id  inner join '
    select @sSQL=@sSql+'Customers p on o.VendorId=p.Id'if cast(@CategoryId as int)>=0
    begin 
    select @sSQL=@sSQL +'where o.CategoryID='+@CategoryId
    end 
    if @orderBy <>'DateTimed'
    select @sSql=@sSql+'Order by o.'+@Orderby +' desc ,o.DateTimed desc &'
    else
    select @sSql=@sSql+ 'order by o'+@orderby +'desc'
    exec (@sSql)
    set nocount offdeclare @cartId varchar(50)SELECT A.ProductID, A.ModelName, A.ModelNumber, B.Quantity, 
          A.UnitCost, CAST(A.UnitCost * B.Quantity AS money) AS ExtendedAmount
    FROM dbo.Products A INNER JOIN
          dbo.ShoppingCart B ON A.ProductID = B.ProductID
    WHERE (B.CartID = @CartID)
    ORDER BY A.ModelName, A.ModelNumber
      

  2.   

    我在SQL2005中存储过程可以建立,但我在VS2005中对存储过程spProductsTop作单元测试时却有这样的提示:
    过程或函数 'spProductsTop' 需要参数 '@top',但未提供该参数。
    没有行受影响。
    (返回 0 行)
    @RETURN_VALUE = 
    完成 [dbo].[spProductsTop] 运行。
    线程 'z2ru7igr0uobi9q [57]' (0x870) 已退出,返回值为 0 (0x0)。
    程序“[1104] [SQL] z2ru7igr0uobi9q: z2ru7igr0uobi9q”已退出,返回值为 0 (0x0)。
    请问这是??
      

  3.   

    top语句 后面加空格看看,还有你一些语句后面需要空格。
      

  4.   

    exec (@sSql)前面加个print @sSql 看看输出
      

  5.   

    ......
    if @orderBy <>'DateTimed'
    select @sSql=@sSql+'Order by o.'+@Orderby +' desc ,o.DateTimed desc &'
    else
    select @sSql=@sSql+ 'order by o'+@orderby +'desc'
    print @sSqlexec (@sSql)
    set nocount off
    .........Sql2005中显示结果为:消息 201,级别 16,状态 4,过程 spProductsTop,第 0 行
    过程或函数 'spProductsTop' 需要参数 '@top',但未提供该参数。(1 行受影响)
      

  6.   

    .........Sql2005中显示结果为:消息 201,级别 16,状态 4,过程 spProductsTop,第 0 行
    过程或函数 'spProductsTop' 需要参数 '@top',但未提供该参数。(1 行受影响)
    是不是要添加个@top参数呀?怎么添加啊