日期:2014-05-16  浏览次数:21279 次

ASP循环读取插入 的问题
想要实现的功能是:
用获得到的人名 查询userinf 表中的 部门字段然后写入到数据库

mysendto(i)是获得到的人名:如 张三,李四,

userinf表
name    userdept
张三      销售部
李四      市场部

目前的代码

<%
sendto=request("TO_NAME")
fsr_user=request("fsr_user")
userdept=request("userdept")
title=request("title")
documenttype=request("lb")
content=request("content")
linkpath=request("files")

dim mysendto       
mysendto=split(sendto,",",-1,1) 

set conn=opendb("oabusy","conn","accessdsn")
set rs1=server.createobject("ADODB.recordset") 
set rs=server.createobject("ADODB.recordset") 
for i=0 to ubound(mysendto) 
if mysendto(i)<>"" then

sql = "Insert Into senddate (title,documenttype,content,sender,recipientusername,获得的部门,filename) Values('"&title&"','"&documenttype&"','"&获得的部门&"','"&content&"','"&fsr_user&"','"&mysendto(i)&"','"&linkpath&"')" 
rs.open Sql,conn,1,1

Response.Write("</br>"&mysendto(i)&"")

 end if
next
rs.close
conn.close
set rs=nothing
set conn=nothing

%>


------解决方案--------------------
for i=0 to ubound(mysendto) 
if mysendto(i)<>"" then
  dept=""
 set dprs=conn.execute("select userdept from userinf where name='"&mysendto(i)&"'")
 if not( dprs.eof or dprs.bof )then dept=dprs(0)
dprs.close

sql = "Insert Into senddate (title,documenttype,content,sender,recipientusername,部门字段,filename) Values('"&title&"','"&documenttype&"','"&dept&"','"&content&"','"&fsr_user&"','"&mysendto(i)&"','"&linkpath&"')" 
rs.open Sql,conn,1,1
 
Response.Write("</br>"&mysendto(i)&"")
 
 end if
next