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

有点难度的问题,关于如何级联删除记录的同时并删除相关图片
数据库ACCESS,建立了表A,表B,表C,表D
字段分别为
a_id,a_pic
b_id,b_aid,b_pic
c_id,c_bid,c_pic
d_id,d_cid,d_pic

其中a_id与b_aid关联,b_id与c_bid关联,c_id与d_cid关联,组成一对多的级联关系,当删除表A的某字段时候,以下各表的相关字段都会被删除,请问怎样可以删除与各表相关的图片?PIC为图片名称。

我只能删除两级关联的记录与响应图片,请问四级关联的应该如何删除?
以下为删除两级关联的代码:

<%
send_id=Request( "c_id ")  
set   conobject=server.CreateObject( "adodb.connection ")  
DBPath=Server.MapPath( "../mydata/mydata.mdb ")  
conobject.open "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= "&DBPath
%>

<%  
sql= "select   *   from   表D   where   d_cid= "&send_id      
set   rs2=server.createobject( "adodb.recordset ")      
rs2.open   sql,conobject,2,2      
while   not   rs2.eof      
Source=   Server.MapPath( "img/ "&rs2( "d_pic "))  
Set   fs=CreateObject( "Scripting.FileSystemObject ")  
fs.DeleteFile   Source,Ture  
Set   fs=Nothing  
rs2.delete          
rs2.movenext      
wend      
rs2.close      
set   rs2=nothing      
%>

<%
sql= "select   *   from   表C   where   c_id= "&send_id      
set   rs3=server.createobject( "adodb.recordset ")      
rs3.open   sql,conobject,2,2      
while   not   rs3.eof      
Source=   Server.MapPath( "img/ "&rs3( "c_pic "))  
Set   fs=CreateObject( "Scripting.FileSystemObject ")  
fs.DeleteFile   Source,Ture  
Set   fs=Nothing        
rs3.delete          
rs3.movenext      
wend      
rs3.close      
set   rs3=nothing
set   fs=nothing        
%>

------解决方案--------------------
有记录图片的路径没有?
有的话那就简单了,根据数据库里需要删除的记录中对的图片路径
用fso删除掉对应图片就成了
------解决方案--------------------
这是删除数据里面的相关记录吧

delete * From (((A inner join B on A.ID=B.a_aid) inner join C on B.ID=C.c_bid) inner join D on C.ID=D.d_cid) where A.ID=1

再删除文件吧

有错还忘请正
------解决方案--------------------
set fso=server.createobject( "scripting.filesystemobject ")

fso.deletefile a表中的pic
conn.execute( "delete from [a] where a_id= "&id)

if conn.execute( "select count(b_id) from [b] where b_aid= "&id)(0)> 0 then
set rs=conn.exeucte( "select * from [b] where b_aid= "&id)
do while not rs.eof
fso.deletefile b表中的pic '先删图片
if conn.execute( "select count(c_id) from [c] where c_bid= "&rs( "b_id "))(0)> 0 then
set rs_c=conn.execute( "select * from [c] where c_bid= "&rs( "b_id "))
do while not rs_c.eof
fso.deletefile c表中的pic
if conn.execute( "select count(d_id) from [d] where d_cid= "&rs_c( "c_id "))(0)> 0 then
set rs_d=conn.execute( "select * from [d] where d_cid= "&rs_c( "c_id "))