这段代码可以解释我的问题,
dim dgv1 as new datagridview
dim dgv2 as new datagridview
dim dgv3 as new datagridview
dim dgv4 as new datagridview
sub processData()
conn = new sqlconnection(strcon)
conn1 = new sqlconnection(byconn)
conn.open
conn1.open
dim trans as sqltransaction = conn.begintransaction
dim trans1 as sqltransaction = conn1.begintransaction
try
dim con1 as new sqlconnection(strconn)
con1.open
dim cmd as new sqlcommand("some query",con1)
cmd.commandtype = commandtype.storedprocedure
dim da as new sqldataadapter(cmd)
dim dt as new datatable
da.fill(dt)
con1.close
dgv1.datasource = dt
for i as integer = 0 to dgv1.rowcount -1
getData2(i)
for j as integer = 0 to dgv2.rowcount -1
getData34()
end for
saveData1(i,conn,trans)
saveData2(conn1,trans1)
next
trans.commit
trans1.commit
conn.close
conn1.close
catch ex as exception
trans.rollback
trans1.rollback
conn.close
conn1.close
end try
end sub
sub getData2(i as integer)
dim con1 as new sqlconnection(strconn)
con1.open
dim cmd as new sqlcommand("some query",con1)
cmd.commandtype = commandtype.storedprocedure
cmd.parameters.addwithvalue("@PARAM",dgv1.item(0,i).value)
dim da as new sqldataadapter(cmd)
dim dt as new datatable
da.fill(dt)
con1.close
dgv2.datasource = dt
end sub
sub getdata34(i as integer)
dim con1 as new sqlconnection(strconn)
con1.open
dim cmd as new sqlcommand("some query",con1)
cmd.commandtype = commandtype.storedprocedure
cmd.parameters.addwithvalue("@PARAM",dgv2.item(0,i).value)
cmd.parameters.addwithvalue("@TYPE","LEFT")
dim da as new sqldataadapter(cmd)
dim dt as new datatable
da.fill(dt)
dgv3.datasource = dt
cmd =new sqlcommand("some query",con1)
cmd.commandtype = commandtype.storedprocedure
cmd.parameters.addwithvalue("@PARAM",dgv2.item(0,i).value)
cmd.parameters.addwithvalue("@TYPE","RIGHT")
da =new sqldataadapter(cmd)
dt =new datatable
da.fill(dt)
dgv4.datasource = dt
con1.close
end sub
sub saveData1(i as integer,cn as sqlconnection, trans as sqltransaction)
'some query with transaction
end sub
sub saveData2(cn as sqlconnection, trans as sqltransaction)
'some query with transaction
end sub
当processData调用时,在函数getData2和getData34中总是给我超时错误消息。在sqlcommand中添加超时没有帮助我。 在存储过程中添加OPTION(RECOMPILE)也无济于事。 也许,我想念一些事情。