2017-11-11 140 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>