数据库中存放的是图片文件的链接,在删除一条记录时,同时想要删除对应的文件。
这里要注意先后顺序,应该先读取原来的文件名,然后用FSO对象删除文件,再删除该条记录,更新操作也是类似。
dim conn,rs,sql,sql1,objFSO,fileurl
set conn = server.createobject("adodb.connection")
str="dsn=conn32;Driver={MySQL ODBC 8.0 ANSI Driver};server=127.0.0.1;uid=×××;pwd=×××;database=weapp"
conn.open(str)'连接MySQL数据库
set rs = server.createobject("adodb.recordset")
sql1 = "select picpath from GoodsInfo where goodsid="&Request.Querystring("id")&""'根据传入id写sql语句
rs.open sql1,conn,1,1'从数据库获取图像的路径信息
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
fileurl = server.mappath("rs("picpath")")
if objFSO.FileExists(fileurl) then '检查文件是否存在
objFSO.DeleteFile(fileurl)'删除指定的文件
end if
Set objFSO = nothing
rs.close
set rs = nothing
sql = "delete from GoodsInfo where goodsid="&Request.QueryString("id")&""
conn.execute(sql)'删除数据库对应的记录
conn.close
set conn = nothing
response.write "<script>alert('删除成功!');location.href='index.asp'</script>"
有任何问题欢迎留言