LINQ to SQL查询进入无限循环

时间:2012-02-09 03:44:39

标签: linq linq-to-sql insert

SP按顺序插入数据,订单明细如下


USE [AdventureWorks]
GO
/****** Object:  StoredProcedure [dbo].[InsertOrder]    Script Date: 02/10/2012 11:36:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[InsertOrder]
     @Comment Varchar(50)
    ,@OrderDate datetime
    ,@CustomerId INT
    ,@Total      FLOAT
    ,@ShipMethod VARCHAR(50)
    ,@ProductIds VARCHAR(200)
AS 
BEGIN 

DECLARE @OrderDetailId    INT 
DECLARE @LoopIndex        INT
DECLARE @Length           INT
DECLARE @CurrentProductId INT

DECLARE @ProductId TABLE 
(
    Id int identity(1,1),
    Data nvarchar(5)
)

BEGIN TRAN

BEGIN TRY 

INSERT INTO [AdventureWorks].[dbo].[Order]
       (
            [Comment]
           ,[OrderDate]
           ,[CustomerId]
           ,[Total]
           ,[ShipMethod]
       )
 VALUES
       (
            @Comment
           ,@OrderDate
           ,@CustomerId
           ,@Total
           ,@ShipMethod
       )

   SET  @OrderDetailId = @@Identity

   PRINT @OrderDetailId



   INSERT INTO @ProductId
   SELECT Data
   FROM  dbo.Split(@ProductIds,',')


   -- SET the values 
   SET @Length = 0 
   SET @LoopIndex = 1 

    SELECT @Length = Count(*) 
    FROM  @ProductId

    WHILE (@LoopIndex <=@Length)
    BEGIN

        PRINT @LoopIndex

        SELECT @CurrentProductId = CONVERT(INT,Data)
        FROM @ProductId
        WHERE Id = @LoopIndex


        PRINT @CurrentProductId

        -- Do the insertion in Order Detail table 
        INSERT INTO [AdventureWorks].[dbo].[OrderDetail]
       (
             [OrderId]
            ,[ProductId]
        )
         VALUES
       (
              @OrderDetailId
            ,@CurrentProductId
       )


        SET @LoopIndex = @LoopIndex + 1


    END

        COMMIT

    END TRY 
    BEGIN CATCH 

        PRINT 'Error'   

    END CATCH 



END

另外,我使用以下代码从WCF插入

public int InsertOrders(Order order, string productIds) {

AdventureWorksDataContext dataContext = new AdventureWorksDataContext(); 
int insertStatus = dataContext.InsertOrder(order.Comment, DateTime.Now, order.CustomerId, order.Total, order.ShipMethod, productIds);
return insertStatus; 
}

0 个答案:

没有答案