请教下如何删除数据库数据同时删除对应的文件 文件上传功能用的gridview显示数据,用RowCommand事件删除对应ID的数据。现在想删除数据的同时删除 fileupload文件夹下对应的文件。fileupload下的文件名存在 数据库中 ptah字段新手,求说的详细些,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的,根据ID去数据库查path,根据path用FileInfo.Delete删除文件 传入要删除的文件路径,File.Delete("***") 是的 先删除存在的文件 再删除数据库的,参考如下代码//获取资源编号 string fielid = context.Request["id"]; string[] fileids = fielid.TrimEnd('|').Split('|');//用|分开各个资源编号 Asiastar.NRBLL.File bll = new Asiastar.NRBLL.File(); foreach( string guid in fileids ) //遍历编号集 { if( bll.FN_SerchPathByFileId(new Guid(guid)) != null || bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count > 0 ) { for( int i = 0 ; i < bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count ; i++ ) { string path = bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows[i]["FilePath"].ToString(); //定义变量接收数据库资源完整路径 if( !Directory.Exists(path) ) //判断文件路径是存在 { try { System.IO.File.Delete(path); //如果路径存在 先删除服务器上的文件 在执行对数据库文件的删除操作 } catch( Exception ex ) { NR.Error.Log.LogType(ex.ToString()); } NRBLL.File nbf = new Asiastar.NRBLL.File(); if( nbf.FN_DeleteRes(new Guid(guid)) == true ) { NR.Error.Log.LogType("删除资源成功!" + path); } else { context.Response.Write("1"); NR.Error.Log.LogType("添加资源失败!" + path); } } } } } context.Response.Write("0"); NR.Error.Log.LogType("添加资源失败!" + path);这个是笔误 删除资源失败- - 1;文件名是id吗?如果是话var path = Server.MapPath("~/fileupload/" + adId); //指定文件路径 if(Directory.Exists(path)){ //该文件是否存在 if (System.IO.Directory.GetFiles(path).Length > 0) //里面是否有内容 { try { System.IO.Directory.Delete(path, true); //干掉文件夹 } catch { } } 关于无限级分类(linq to sql) 如何在repeater内实现超级链接? 关于服务器端注册的脚本的位置 在repeater等控件里面再如何编程啊? 请问 Visual Studio.NET 2003 免费下载? 谁能解释一下<%# %>,<%$ %>,<%@ %>的用法? C#后台备份Sql2008数据库出现问题 请大家推荐一些著名的软件公司 SVN里下的代码在vs上打开问题 LINQ to Entities GroupBy之后不能用Distinct? weboffice在网页中得显示和本地安装的office版本有关联么 asp.net中listview绑定数据的问题
参考如下代码//获取资源编号
string fielid = context.Request["id"];
string[] fileids = fielid.TrimEnd('|').Split('|');//用|分开各个资源编号
Asiastar.NRBLL.File bll = new Asiastar.NRBLL.File();
foreach( string guid in fileids ) //遍历编号集
{
if( bll.FN_SerchPathByFileId(new Guid(guid)) != null || bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count > 0 )
{
for( int i = 0 ; i < bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count ; i++ )
{
string path = bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows[i]["FilePath"].ToString(); //定义变量接收数据库资源完整路径
if( !Directory.Exists(path) ) //判断文件路径是存在
{
try
{
System.IO.File.Delete(path); //如果路径存在 先删除服务器上的文件 在执行对数据库文件的删除操作
} catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
NRBLL.File nbf = new Asiastar.NRBLL.File();
if( nbf.FN_DeleteRes(new Guid(guid)) == true )
{
NR.Error.Log.LogType("删除资源成功!" + path);
}
else
{
context.Response.Write("1");
NR.Error.Log.LogType("添加资源失败!" + path);
}
}
}
}
}
context.Response.Write("0");
如果是话
var path = Server.MapPath("~/fileupload/" + adId); //指定文件路径
if(Directory.Exists(path)){ //该文件是否存在
if (System.IO.Directory.GetFiles(path).Length > 0) //里面是否有内容
{
try
{
System.IO.Directory.Delete(path, true); //干掉文件夹
}
catch
{
}
}