在Web应用程序中,我有一个gridview,每行包含3个文本框,gridview包含127行。我必须将每个文本框的数据插入到数据库中,为此我创建了一个数据表,我将每个文本框的数据收集到该数据表中,并将数据表转换为xml格式,然后插入到数据库中,但它给出了性能问题[事实上它给出了超时的例外],是否有任何好的过程将数据插入数据库。谢谢。
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
答案 0 :(得分:1)
最好在从GridView读取行时插入记录。当然,您可以选择stored-procedure
或parameterized
查询。
答案 1 :(得分:0)
首先,您应该优化查询,因为它似乎很慢,这就是您获得此异常的原因。其次,您可以将命令超时增加到无限以克服此异常。
command.CommandTimeout=0;
在http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx
了解详情