VB程序员博客
dim cn as new adodb.connection
cn.open ""
str1="update tab set a=1 where …"
str2="update tab set a=12 where …"
str3="update tab set a=12 where …"
str4="update tab set a=12 where …"
str5="update tab set a=12 where …"
…
cn.execute str1
cn.execute str2
cn.execute str3
cn.execute str4
cn.execute str5
…
现在想把…包含的部分变成下列形式(但是下列的形式是错误的),请问如何实现。或帮我改易下页可以。
for i=0 to 2000
cn.execute str & (i+1)
next
为什么要写成上面的形式,我觉得这样好修改比较方便。另外不占地方.
dim str1(1 to 5) as string ' str是系统函数,所以用str1
str1(1)="update tab set a=1 where …"
str1(2)="update tab set a=12 where …"
str1(3)="update tab set a=12 where …"
str1(4)="update tab set a=12 where …"
str1(5)="update tab set a=12 where …"
for i=0 to 2000
cn.execute str1(i+1)
next
for i=0 to 2000
str="update tab set a=" & (i+1) & " where …"
cn.execute str
next
for i=0 to 2000
strsql="update tab set a=" & (i+1) & " where …"
cn.execute strsql
next
错误原因在于VB不支持直接使用指针。
延续1楼的思路,使用更有效率的批更新:
dim str1(1 to 5) as string ' str是系统函数,所以用str1
str1(1)="update tab set a=1 where …"
str1(2)="update tab set a=12 where …"
str1(3)="update tab set a=12 where …"
str1(4)="update tab set a=12 where …"
str1(5)="update tab set a=12 where …"
dim UpdateString as string
UpdateString = "BEGIN TRANSACTION" & chr(13) & chr(10)
for i=0 to 2000
updatestring=updatestring & str1(i+1) & chrw(13) & chrw(10)
next
updatestring = updatestring & "COMMIT TRANSACTION"
cn.execute updatestring