日期:2014-05-17  浏览次数:20899 次

数组插入sql表中
现在有一个字符串数组 "2,23,24,55,"
想把它插入一个表中,每一个字符插入一行;
现在我用的方法是:循环整个数组一条一条的插入,但是在页面运行的时候速度非常慢;大家有什么好的方法和建议吗
例如:
 ss="2,23,24,55,"
 aryReturn = Split(ss,",")
  For i = LBound(aryReturn) To UBound(aryReturn)-1 '循环博客id把成员加入到成员表
  '去除重复的邀请
   
ConnMain.execute(" insert into users (num,time) values('"&aryReturn(i)&"',getdate()") 

  Next 
 

------解决方案--------------------
VBScript code
str = "2,23,24,55,"
str = left(str,len(str)-1)
sql = "insert into users (num,time) select "
sql = sql & Replace(str,",",", getdate() union all select ") & ",getdate()"
ConnMain.execute(sql)

------解决方案--------------------
用批处理方式,一次递交
VBScript code

<%
ss = "2,23,24,55,"
ss = Replace(ss, " ", "")
aryReturn = Split(ss,",")

sql = "SELECT [num], [time] FROM [users] WHERE 1=0"
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.Open sql, ConnMain, 1, 4
For i = 0 To UBound(aryReturn)
    If aryReturn(i)<>"" Then
        rs.AddNew
        rs("num").Value = aryReturn(i)
        rs("time").Value = Now()
     End If
Next
rs.UpdateBatch
rs.Close
Set rs = Nothing
ConnMain.Close
Set ConnMain = Nothing
%>