ASP批量导入数据
原理:就是用一个FOR循环朝数据库里面插入数据,但是过程比较复杂,但是用到数据库的存储过程的时候要用到事务,以防止插入过程中数据出错。
stuinput2.asp 页面部分:
<%option explicit%>
<%
dim schname,classname,classid,schid
schname=""
classname=""
classid=""
schid=""
schname=ubb(request.querystring("schname"))
classname=ubb(request.querystring("classname"))
classid=ubb(request.querystring("classid"))
schid=ubb(request.querystring("schid"))
%>
导入学生
数据处理及提交部分:
<%option explicit%>
<%
Dim examsco,schid,returnvalue,temp,i,templist2,stuid,stuname,stumobile,stumobilelist,stumobile2,studentid,classid,k,errormsg,noinnum,errormobnum,errstunum,schname,classname
classid=""
examsco=""
schid=""
schname=""
classname=""
'Response.Write("1111")
examsco=ubb(Request.Form("txta_examsco"))
schid=ubb(Request.Form("hid_schid"))
classid=ubb(Request.Form("hid_classid"))
schname=ubb(Request.Form("hid_schname"))
classname=ubb(Request.Form("hid_classname"))
if examsco="" then
response.write("")
response.End
End if
examsco=replace(examsco,chr(13) & chr(10),"|@|")
examsco=replace(examsco," ","|#|")
examsco=replace(examsco,"'","")
examsco=examsco&"|#||@|"
'Response.Write(examsco)
'response.End
returnvalue=""
returnvalue=checkdate(examsco)
if returnvalue="1" then
response.write("")
response.end
elseif returnvalue="2" then
response.write("")
response.end
elseif returnvalue="3" then
response.write("")
response.end
end if
temp=split(examsco,"|@|") '分行
for i=0 to ubound(temp)-1
templist2=split(temp(i),"|#|")'分字段
stuid=""
stuname=""
stumobile=""
stuid=trim(templist2(0))
stumobile=trim(templist2(1))
if stumobile<>"" then
if right(stumobile,1)="/" then
stumobile=left(stumobile,len(stumobile)-1)
end if
stumobilelist=""
stumobilelist=split(stumobile,"/")
if ubound(stumobilelist)=0 then '如果为一个号码时
stumobile2=""
stumobile2=stumobile
if len(stumobile2)=8 then
stumobile2="0512"&stumobile2
end if
if isnull(studentid) or studentid="" then
returnvalue=""
returnvalue=addstudent(stuid,stumobile2,schid)
if returnvalue="0" then
k=k+1
elseif returnvalue="9" then
noinnum=noinnum+1
elseif returnvalue="6" then
errormobnum=errormobnum+1
else
errstunum=errstunum+1
end if
else
returnvalue=""
returnvalue=addmobile(studentid,stumobile2)
'response.write(" returnvalue1=" & returnvalue & " ")
if returnvalue<>"0" then
noinnum=noinnum+1
'response.write("")
'exit for
'response.end
end if
end if
else '多个号码
for j=0 to ubound(stumobilelist)
stumobile2=""
stumobile3=""
stumobile2=stumobilelist(j)
stumobile3=stumobilelist(0)
if len(stumobile3)=8 then stumobile3="0512"&stumobile3
if len(stumobile2)=8 then
stumobile2="0512"&stumobile2
end if
'response.write("sdf"&stumobile2)
studentid=""
'response.write("classid="&classid&"stumobile2="&stumobile2)
studentid=getstudentid(classid,stumobile3)
'studentid=getstudentid("17862","13913568488")
'response.write("studentid="&studentid&" ")
if isnull(studentid) or studentid="" then
returnvalue=""
returnvalue=addstudent(stuid,stuname,stuphone,stumobile2,classid)
'response.write(" returnvalue=" & returnvalue & " ")
'response.write(stumobile2)
'response.end
if returnvalue="0" then
k=k+1
elseif returnvalue="9" then
noinnum=noinnum+1
elseif returnvalue="6" then
errormobnum=errormobnum+1
'else
'response.write("")
'exit for
'response.end
else
errstunum=errstunum+1
end if
else
returnvalue=""
returnvalue=addmobile(studentid,stumobile2)
'response.write(" returnvalue1=" & returnvalue & " ")
if returnvalue<>"0" then
noinnum=noinnum+1
'response.write("")
'exit for
'response.end
end if
end if
next
'response.end
end if
else
returnvalue=""
returnvalue=addstudent(stuid,stuname,stuphone,stumobile,classid)
'response.write(" returnvalue=" & returnvalue & " ")
if returnvalue="0" then
k=k+1
elseif returnvalue="9" then
noinnum=noinnum+1
elseif returnvalue="6" then
errormobnum=errormobnum+1
'else
'response.write("")
'exit for
'response.end
else
errstunum=errstunum+1
'errstunum="dssss"
end if
end if
next
'response.write("共有"&k&"条记录导入数据库!")
errormsg=""
if k>0 then
errormsg="成功导入"&k&"条学生记录\n"
end if
'if noinnum<>"0" then
'errormsg=errormsg & "有"&noinnum&"条学生或号码记录原来已经存在于本校,本次不能导入\n"
'end if
if errormobnum>0 then
errormsg=errormsg & "有无效号码"&errormobnum&"个未能导入\n"
end if
if errstunum>0 then
errormsg=errormsg & "有"&errstunum&"个学生导入失败\n"
end if
response.write("")
%>
<%
function checkdate(scotext)
dim chk_templist,chk_templist1
dim chk_stuid,chk_stumobile
dim chk_moblist
dim chk_i
chk_templist=split(scotext,"|@|")
for i=0 to ubound(chk_templist)-1
chk_templist1=split(chk_templist(i),"|#|")
if ubound(chk_templist1)<1 then
checkdate="1"
exit for
else
chk_stuid=""
chk_stumobile=""
chk_stuid=trim(chk_templist1(0))
chk_stumobile=trim(chk_templist1(1))
if isnumeric(chk_stuid)=false then
checkdate="2"
exit for
else
if chk_stumobile<>"" then
if right(chk_stumobile,1)="/" then
chk_stumobile=left(chk_stumobile,len(chk_stumobile)-1)
end if
if instr(chk_stumobile,"/")>0 then
chk_moblist=split(chk_stumobile,"/")
for chk_i=0 to ubound(chk_moblist)
if len(chk_moblist(chk_i))=8 then
chk_moblist(chk_i)="0512"&chk_moblist(chk_i)
end if
'--------------------------------------------------
'if len(chk_moblist(chk_i))="11" and left(chk_moblist(chk_i),3)="512" then
' chk_moblist(chk_i)="0"&chk_moblist(chk_i)
'end if
'----------------------------------------------------
if checkmobile(chk_moblist(chk_i))="bad" then
checkdate="3"
exit for
else
checkdate="0"
end if
next
else
if len(chk_stumobile)=8 then
chk_stumobile="0512"&chk_stumobile
end if
'------------------------------------------
'if len(chk_stumobile)=11 and left(chk_stumobile,3)="512" then
' chk_stumobile="0"&chk_stumobile
'end if
'---------------------------------------------
if checkmobile(chk_stumobile)="bad" then
checkdate="3"
exit for
else
checkdate="0"
end if
end if
else
checkdate="0"
end if
end if
end if
next
end Function
function addstudent(stuid,stumobile,schid)
'Response.Write(stuid)
'Response.End
dim fun_returnvalue
set cmd=Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = connstr
cmd.commandtext="dbo.jxt_stumobile_insert"
cmd.commandtype=4
cmd.parameters.append cmd.createparameter("@schid",adinteger,adparaminput,4,schid)
cmd.parameters.append cmd.createparameter("@studentid",advarchar,adparaminput,15,stuid)
cmd.parameters.append cmd.createparameter("@mobile",advarchar,adparaminput,20,stumobile)
cmd.parameters.append cmd.createparameter("@Ret",adinteger,adparamoutput)
cmd.execute()
fun_returnvalue=""
fun_returnvalue=cmd.parameters("@Ret").value
set cmd=nothing
if fun_returnvalue="0" then
call logopera(session("se_userid"),"stuinput2.asp","stuinput2_action.asp",session("se_userid")&"给"&classid&"导入职校学生手机号码!")
ElseIf fun_returnvalue="1" then
Response.Write("")
Response.End
ElseIf fun_returnvalue="2" Then
Response.Write("")
Response.End
ElseIf fun_returnvalue="3" Then
Response.Write("")
Response.End
ElseIf fun_returnvalue="4" Then
Response.Write("")
Response.End
end if
addstudent=fun_returnvalue
end function
%>
存储过程
ALTER PROCEDURE [dbo].[jxt_stumobile_insert]
@schid int,
@studentid nvarchar(15),
@mobile nvarchar(20),
@ret int output
as
begin
declare @status int
begin
if dbo.FC_CheckMobileType(@mobile)='yd'
begin
set @status=12
end
else if dbo.FC_CheckMobileType(@mobile)='lt'
begin
set @status=22
end
else if dbo.FC_CheckMobileType(@mobile)='xlt'
begin
set @status=32
end
else if dbo.FC_CheckMobileType(@mobile)='cdma'
begin
set @status=52
end
end
if not exists(select top 1 * from jxt_student where studentid=''+@studentid+'')
begin
set @ret=1 --学生编号不存在
end
else if len(@studentid)<>10
begin
set @ret=2 --学生编号长度不正确
end
else if len(@mobile) not in('11','12')
begin
set @ret=3 --手机号码不正确
end
else if exists(select * from Jxt_Student_Mobile where studentid=@studentid and mobile=@mobile)
begin
set @ret=4 --该条数据已经存在
end
else
begin
begin transaction
insert into Jxt_Student_Mobile (studentid,mobile,status,[path],schid)
values(@studentid,@mobile,@status,@status,@schid)
if @@error<>0
begin
rollback transaction
set @ret=5 --数据插入成功
end
set @ret=0
commit transaction
end
end