2017-11-11 139 views
1

有一个在我的数据库中的问题,当我执行此: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

+0

在'的ConnectionStrings:DefaultConnection',你有'初始Catalog'集?如果是这样,是否设置为'[Product]'表所在的相同数据库? 'dbo'模式中的'[Product]'表? – tarheel

回答

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>