从文件夹删除图像的问题 [英] problem with delete image from folder
问题描述
有人可以帮助我吗?
SqlConnection con = 新 SqlConnection(connection.connectionStr);
SqlDataAdapter da = 新 SqlDataAdapter(" + txtCode.Text.Trim()+ '",con );
DataTable dt = 新 DataTable();
da.Fill(dt);
字符串 str = dt.Rows [ 0 ] [ 0 ].ToString();
System.IO.File.Delete(Server.MapPath(" )+ " + str);
System.IO.File.Delete(Server.MapPath(" )+ " + str);
它 错误 是:
可以 不能 找到 的一部分 的的一部分 路径'C:\ Documents 和设置\ smm \ Desktop \ ESFAHAN_WOODS.COM \ admin_admin \ all_images \ Gallery \ smallImage \ 1.jpg".
但 为什么 我 是无效的???
我们在猜测您收到什么错误吗?!
好吧,我的猜测是您的路径无效.
顺便说一句,正确猜想的奖励是什么?
首先,不要那样做:使用参数化查询代替:
SqlDataAdapter da = 新 SqlDataAdapter(" ,等等); da.SelectCommand.Parameters.AddWithValue(" ,txtCode.Text.Trim());您的工作方式使您更容易受到SQL注入攻击的攻击,这些攻击可能会意外或故意破坏数据库.
其次,检查您的表中是否包含数据-如果用户键入错误的名称会怎样?砰!错误对象未设置为实例...",您的代码崩溃了.盲目使用"[0] [0]"而不进行检查是愚蠢的,毫无意义的用户时间浪费.
第三,检查从数据库返回的数据-由于我不知道您的表是什么样,所以我不确定,但是dt.Rows [0] [0] .ToString很有可能()返回的是数据类型名称,而不是文件名称.
最后,当心相对路径!如果您的页面不在您的根目录中(可能不应该),那么您需要查看路径的前缀〜/",以准确指定文件夹的路径.
它的错误是:
找不到路径``C:\ Documents and Settings \ smm \ Desktop \ ESFAHAN_WOODS.COM \ admin_admin \ all_images \ Gallery \ smallImage \ 1.jpg''的一部分.
但是为什么我的路径无效???
hi
when i want delete my image from folder my code not work and make error
anybody can help me??
SqlConnection con = new SqlConnection(connection.connectionStr);
SqlDataAdapter da=new SqlDataAdapter ("select productImage from companyCategory where productCategoryID='" + txtCode.Text.Trim() + "'",con);
DataTable dt = new DataTable();
da.Fill(dt);
string str = dt.Rows[0][0].ToString();
System.IO.File.Delete(Server.MapPath("all_images/Gallery/smallImage") + "/" + str);
System.IO.File.Delete(Server.MapPath("all_images/Gallery") + "/" + str);
it error is:
Could not find a part of the path 'C:\Documents and Settings\smm\Desktop\ESFAHAN_WOODS.COM\admin_admin\all_images\Gallery\smallImage\1.jpg'.
but why my path is invalid????
Are we to guess what error you receive?!
Well ok, my guess is your path is invalid.
Btw, what is the prize for a correct guess?
First off, don''t do it like that: use a parametrized query instead:
SqlDataAdapter da=new SqlDataAdapter ("select productImage from companyCategory where productCategoryID=@PC", con); da.SelectCommand.Parameters.AddWithValue("@PC", txtCode.Text.Trim());The way you are working leaves you wide open to SQL injection attacks which can accidentally or deliberately destroy your database.
Secondly, check your table contains data - what happens if the user types the name wrong? Bang! "Object not set to an instance..." error and your code falls over. Blindly using "[0][0]" without checking is a stupid, pointless waste of user time.
Thirdly, check you data coming back from the DB - Since I don''t know what your table looks like, I can''t be sure, but there is a good chance that dt.Rows[0][0].ToString() is returning a datatype name, rather than a file name.
Finally, beware of relative paths! If your page is not in your root, (and it probably shouldn''t be) then you need to look at prefixing your path with "~/" to specify exactly the path to your folders.
it error is:
Could not find a part of the path ''C:\Documents and Settings\smm\Desktop\ESFAHAN_WOODS.COM\admin_admin\all_images\Gallery\smallImage\1.jpg''.
but why my path is invalid????
这篇关于从文件夹删除图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!