optimisation insertion en bdd
j'ai une boucle et 2 objets adodb
il vaut mieux redéclarer mon objet ADODB.Command à chaque fois
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| ' Declare all variables.
Option Explicit
Dim conn, connstring, rec2, rs2
Dim rec1, rs1
Dim MyCommand
Dim strBody
set conn = createobject("adodb.connection")
connstring = "driver={SQL SERVER};" & "server=serveur;uid=sa;pwd=;database=base"
conn.open connstring
rec2="SELECT Id FROM maTable"
set rs2=conn.execute(rec2)
if rs2.eof=false then
rs2.movefirst
do while not rs2.eof
strBody = strBody&"<br>n° : "&rs2("Id")
Set MyCommand = CreateObject("ADODB.Command")
Set MyCommand.ActiveConnection = conn
MyCommand.CommandText = "INSERT INTO Memo (MemoText,CreatedOn,CreatedById,inquiryId) VALUES('Relance par mail automatique',getdate(),'IU:ADMIN',?)"
MyCommand.CommandType = 1
MyCommand.Parameters(0).Value = rs2("Id")
MyCommand.Execute
Set MyCommand=nothing
rs2.movenext
loop
end if
conn.close
set conn=nothing |
ou bien une seule fois :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| ' Declare all variables.
Option Explicit
Dim conn, connstring, rec2, rs2
Dim strBody
Dim rec1, rs1
Dim MyCommand
set conn = createobject("adodb.connection")
connstring = "driver={SQL SERVER};" & "server=serveur;uid=sa;pwd=;database=base"
conn.open connstring
rec2="SELECT Id FROM maTable"
'obj pour l'insertion dans memo
Set MyCommand = CreateObject("ADODB.Command")
Set MyCommand.ActiveConnection = conn
MyCommand.CommandText = "INSERT INTO SMemo (MemoText,CreatedOn,CreatedById,inquiryId) VALUES('Relance par mail automatique',getdate(),'ADMIN',?)"
MyCommand.CommandType = 1
set rs2=conn.execute(rec2)
if rs2.eof=false then
rs2.movefirst
do while not rs2.eof
strBody = strBody&"<br>n° : "&rs2("Id")
MyCommand.Parameters(0).Value = rs2("Id")
MyCommand.Execute
rs2.movenext
loop
end if
Set MyCommand=nothing
conn.close
set conn=nothing |