开发者

How to insert data from gridview to database?

in web application, i have a gridview in that each row contain 3 textboxes and gridview contain 127 rows. I have to insert the data of each textbox into database, for this i created a data table and i collect the data of each textbox into that data table and convert the data table into xml form, then inserted into database, but it is g开发者_开发问答iving performence issue[infact it is giving time out exeception], is there any good process to insert the data into database. thank you.

        CREATE procedure [dbo].[USP_RollPlan_InsertProducts](@xmldata xml)                  
    as                  
    begin      
    declare @rollingplainid int      
    declare @xproductcode varchar(30)    
    declare @xQantity1 decimal    
    declare @xRollplanyear int    
    declare @xRollplanmonthYear date    
    declare @xempid varchar(8)    
    declare @xsession varchar(60)    
    declare @xcandflocation int    

    SELECT                         
    cast(convert(nvarchar(max),colx.query('data(productcode)')) as varchar(30)) as xproductcode,                        
    cast(convert(nvarchar(max),colx.query('data(Qantity1)')) as decimal) as xQantity1,    
    cast(convert(nvarchar(max),colx.query('data(Rollplanyear)')) as int) as xRollplanyear,    
    cast(convert(nvarchar(max),colx.query('data(RollplanmonthYear)')) as date) as xRollplanmonthYear,    
    cast(convert(nvarchar(max),colx.query('data(empid)')) as varchar(8)) as xempid,    
    cast(convert(nvarchar(max),colx.query('data(session)')) as varchar(60)) as xsession,    
    cast(convert(nvarchar(max),colx.query('data(candflocation)')) as int) as xcandflocation    
    INTo #tmpES FROM @xmldata.nodes('DocumentElement/Mytable') AS Tabx(Colx)     


    declare db_cursor cursor for     

    select  xproductcode,xQantity1,xRollplanyear,xRollplanmonthYear,xempid,xsession,xcandflocation from #tmpES    

    open db_cursor    
    fetch next from db_cursor into @xproductcode,@xQantity1,@xRollplanyear,@xRollplanmonthYear,@xempid,@xsession,@xcandflocation     

    while @@FETCH_STATUS =0    
    begin                           

    select  @rollingplainid = max(rollingplanid) from Tbl_F_Roll_PlanHeader_T where  empid=@xempid            
    if not exists ( select * from  Tbl_F_Roll_PlanDetails_T where CreatedBy =@xempid and ProductCode =@xproductcode and RollingPlanId=@rollingplainid   and RollPlanMonthYear =@xRollplanmonthYear     and CandFLocation =@xcandflocation and Status=1 )    


    begin      

        insert into Tbl_F_Roll_PlanDetails_T(rollingplanid,productcode,rollplanmonthyear,rollplanyear,candflocation,quantity,CreatedBy,CreatedOn,sessionid,status)                  
        values(@rollingplainid ,@xproductcode ,@xRollplanmonthYear ,@xRollplanyear ,@xcandflocation ,@xQantity1,@xempid,GETDATE (),@xsession,1)                  
    end              
    else              
    begin        
        if(@xQantity1 =0)        
        begin        
            delete from Tbl_F_Roll_PlanDetails_T where ProductCode=@xproductcode and RollingPlanId =@rollingplainid and CandFLocation =@xcandflocation and RollPlanMonthYear =@xRollplanmonthYear and RollPlanYear =@xRollplanyear         
        end                   
            update Tbl_F_Roll_PlanDetails_T set quantity=@xQantity1,CreatedOn =GETDATE() where ProductCode =@xproductcode and DATEDIFF (dd, RollPlanMonthYear ,@xRollplanmonthYear)=0 and CandFLocation =@xcandflocation and CreatedBy =@xempid                
        end                 

    fetch next from db_cursor into  @xproductcode,@xQantity1,@xRollplanyear,@xRollplanmonthYear,@xempid,@xsession,@xcandflocation     
    end     
    close db_cursor    
    deallocate db_cursor             
    end  


It's better to insert a record while reading rows from GridView. Of course you can choose stored-procedure or parameterized query.


First, you should optimize the query as it seems slow and thats why you are getting this exception. Second, you can increase the command timeout to infinite to overcome this exception.

command.CommandTimeout=0;

See more at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜