SQL Server无法执行

时间:2017-11-11 03:49:31

标签: asp.net sql-server

执行此操作时,我的数据库出现问题:

<asp:SqlDataSource ID="SqlDataSourceCombine" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
     DeleteCommand="DELETE FROM [Product] WHERE [ProductId] = @ProductId" 
     InsertCommand="INSERT INTO [Product] ([ProductName], [ProductDescription], [ProductCategory], [ProductBrand], [ProductPrice], [ProductQty], [ProductUploadDate]) VALUES (@ProductName, @ProductDescription, @ProductCategory, @ProductBrand, @ProductPrice, @ProductQty, @ProductUploadDate)"  
     SelectCommand="SELECT Product.*, ProductCategory.*, ProductBrand.*
                    FROM Product 
                    INNER JOIN ProductCategory AS PC ON Product.ProductCategory = ProductCategory.CategoryId     
                    INNER JOIN ProductBrand AS PB ON Product.ProductBrand = ProductBrand.BrandId" 
     UpdateCommand="UPDATE [Product] SET [ProductName] = @ProductName, 
                        [ProductDescription] = @ProductDescription, 
                        [ProductCategory] = @ProductCategory, 
                        [ProductBrand] = @ProductBrand, 
                        [ProductPrice] = @ProductPrice, 
                        [ProductQty] = @ProductQty, 
                     WHERE [ProductId] = @ProductId">
</asp:SqlDataSource>

我收到此错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

您指定的查询中存在多个错误。

  • 首先,您的SELECT查询在最后一次内连接之前使用Product,这是错误的语法。因此,请对select命令使用以下查询。

SELECT查询

SELECT product.*, 
       productcategory.*, 
       productbrand.* 
FROM   product 
       INNER JOIN productcategory 
               ON product.productcategory = productcategory.categoryid 
       INNER JOIN productbrand 
               ON product.productbrand = productbrand.brandid 
  • 其次,您的UPDATE查询在WHERE之前有一个逗号,这又是错误的语法。使用下面的查询来获取更新命令。

更新查询

UPDATE [product] 
SET    [productname] = @ProductName, 
       [productdescription] = @ProductDescription, 
       [productcategory] = @ProductCategory, 
       [productbrand] = @ProductBrand, 
       [productprice] = @ProductPrice, 
       [productqty] = @ProductQty 
WHERE  [productid] = @ProductId 

您的最终标记应如下所示。

带有更正查询的SqlDataSource

<asp:SqlDataSource ID="SqlDataSourceCombine" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
    DeleteCommand="DELETE FROM [Product] WHERE [ProductId] = @ProductId" 
    InsertCommand="INSERT INTO [Product] ([ProductName], [ProductDescription], [ProductCategory], [ProductBrand], [ProductPrice], [ProductQty], [ProductUploadDate]) VALUES (@ProductName, 
       @ProductDescription, @ProductCategory, @ProductBrand, @ProductPrice, @ProductQty, @ProductUploadDate)"  
   SelectCommand="SELECT product.*,  productcategory.*, 
     productbrand.*  FROM   product 
   INNER JOIN productcategory 
   ON product.productcategory = productcategory.categoryid 
   INNER JOIN productbrand 
   ON product.productbrand = productbrand.brandid "
   UpdateCommand="UPDATE [product] 
    SET    [productname] = @ProductName, 
    [productdescription] = @ProductDescription, 
    [productcategory] = @ProductCategory, 
    [productbrand] = @ProductBrand, 
    [productprice] = @ProductPrice, 
    [productqty] = @ProductQty 
    WHERE  [productid] = @ProductId ">
</asp:SqlDataSource>