使用ASP.NET C#中的gridview从文件夹和数据库中删除图像 [英] Delete image from folder and database using gridview in ASP.NET C#
问题描述
我可以从数据库中删除,但我不知道如何从文件夹中删除图片。
请帮助。
为了更好地理解,请编辑我的代码并粘贴。
提前致谢。
我尝试过:
protected void Button1_Click(object sender,EventArgs e)
{
SqlCommand command = new SqlCommand(SPinsertitem,con);
command.CommandType = CommandType.StoredProcedure;
//上传图片1 //
string strname = picture1.FileName.ToString();
strname =〜//品牌//新文件夹//+ strname;
picture1.PostedFile.SaveAs(Server.MapPath(strname));
command.Parameters.Add(@ imageone,SqlDbType.VarChar,100).Value = strname;
// UPLOAD IMAGE 2 //
string strname2 = picture2.FileName.ToString();
strname2 =〜//品牌//新文件夹//+ strname2;
picture2.PostedFile.SaveAs(Server.MapPath(strname2));
command.Parameters.Add(@ imagetwo,SqlDbType.VarChar,100).Value = strname2;
//上传图片3 //
string strname3 = picture3.FileName.ToString();
strname3 =〜//品牌//新文件夹//+ strname3;
picture3.PostedFile.SaveAs(Server.MapPath(strname3));
command.Parameters.Add(@ imagethree,SqlDbType.VarChar,100).Value = strname3;
protected void BindGrid ()
{
SqlDataAdapter myCommand = new SqlDataAdapter(SELECT * FROM pikiphones,con);
DataSet ds = new DataSet();
myCommand.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void delete_rOW(object sender,GridViewDeleteEventArgs e)
{
String deleteCmd =DELETE来自pikiphones WHERE id = @id;
SqlCommand myCommand = new SqlCommand(deleteCmd,con);
myCommand.Parameters.Add(new SqlParameter(@ id,SqlDbType.Int));
myCommand.Parameters [@ id]。Value = Convert.ToInt32(GridView1.Rows [e.RowIndex] .Cells [1] .Text);
try
{
myCommand.ExecuteNonQuery();
}
catch(SqlException)
{
Label1.Text =错误:无法删除记录;
}
myCommand.Connection.Close();
BindGrid();
}
我看不到你试图删除文件的地方。
你可以使用Systme.IO.File.Delete(path);
,但无论如何都不需要将文件保存到文件系统:
将图像字段设置为varbinary(最大值):
创建 表图像(
image_id int 不 null identity ( 1 , 1 ) primary key ,
image_image1_name nvarchar (max),
image_image1_binary varbinary (max)
- ...
)
然后直接从上传中读取文件流。无需保存,只需将其作为byte []保存在内存中几个周期。
// ....
byte [ ] input = new byte [picture1.ContentLength];
var stream = picture1.InputStream;
stream.Read(输入, 0 ,picture1.ContentLength);
command.Parameters.Add( @ image1Name,SqlDbType.VarChar , 100 )。Value = picture1.FileName;
command.Parameters.Add( @ image1Byte,SqlDbType.VarBinary,file.Length ).Value =输入;
这就是我在整个地方上传文件的方式。清理起来要快得多,也不那么麻烦,因为当你完成它时GC会把文件分开。
我希望有帮助^ _ ^
最后解决方案的附录:
好的 - 首先你需要尝试删除该项目,最好在参考之前,但它没有无所谓。只需确保在删除条目之前获得路径:
protected void delete_rOW( object sender,GridViewDeleteEventArgs e)
{
// 我会写这个最佳实践
var id = Convert.ToInt32(GridView1.Rows [e.RowIndex] .Cells [ 1 ] .Text)
string selectCmd = 选择来自pikiphones的路径WHERE id = @id
使用(SqlCommand com = con.CreateCommand){
com.Text = selectCmd ;
com.Parameters.Add( new SqlParameter( @id,SqlDbType.Int){Value = id});
var path = com.ExecuteScalar()。toString();
if (!string.isNullOrEmpty(path)&& Path.Exists(path)){
// try catch?
Path.Delete(path);
}
}
字符串 deleteCmd = DELETE FROM pikiphones WHERE id = @id;
SqlCommand myCommand = new SqlCommand(deleteCmd,con);
myCommand.Parameters.Add( new SqlParameter( @id,SqlDbType.Int));
myCommand.Parameters [ @ id]。Value = id;
尝试
{
myCommand.ExecuteNonQuery();
}
catch (SqlException)
{
Label1.Text = 错误:无法删除记录;
}
myCommand.Connection.Close();
BindGrid();
}
您可能需要调试它。我知道我有一些方法关键字错了。如果你遇到困难,请告诉我^ _ ^
I am able to delete from database but i do not know how delete image from folder.
Please help .
for better understanding please edit my code and paste.
Thanks in advance.
What I have tried:
protected void Button1_Click(object sender, EventArgs e) { SqlCommand command = new SqlCommand("SPinsertitem", con); command.CommandType = CommandType.StoredProcedure; // UPLOAD IMAGE 1 // string strname = picture1.FileName.ToString(); strname = "~//brand//New folder//" + strname; picture1.PostedFile.SaveAs(Server.MapPath(strname)); command.Parameters.Add("@imageone", SqlDbType.VarChar, 100).Value = strname; // UPLOAD IMAGE 2 // string strname2 = picture2.FileName.ToString(); strname2 = "~//brand//New folder//" + strname2; picture2.PostedFile.SaveAs(Server.MapPath(strname2)); command.Parameters.Add("@imagetwo", SqlDbType.VarChar, 100).Value = strname2; // UPLOAD IMAGE 3 // string strname3 = picture3.FileName.ToString(); strname3 = "~//brand//New folder//" + strname3; picture3.PostedFile.SaveAs(Server.MapPath(strname3)); command.Parameters.Add("@imagethree", SqlDbType.VarChar, 100).Value = strname3;
protected void BindGrid() { SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM pikiphones", con); DataSet ds = new DataSet(); myCommand.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); }
protected void delete_rOW(object sender, GridViewDeleteEventArgs e) { String deleteCmd = "DELETE FROM pikiphones WHERE id= @id"; SqlCommand myCommand = new SqlCommand(deleteCmd, con); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)); myCommand.Parameters["@id"].Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text); try { myCommand.ExecuteNonQuery(); } catch (SqlException) { Label1.Text = "ERROR: Could not delete record"; } myCommand.Connection.Close(); BindGrid(); }
I see nowhere where you try to delete the file.
You can just useSystme.IO.File.Delete(path);
, but there is no need to save the file to the file system anyway:
set up the image field as a varbinary(max):
Create Table images( image_id int not null identity(1,1) primary key, image_image1_name nvarchar(max), image_image1_binary varbinary(max) -- ... )
Then read the file stream directly from the upload. No need to save it, just keep it in memory as a byte[] for a couple of cycles.
//.... byte[] input = new byte[picture1.ContentLength]; var stream = picture1.InputStream; stream.Read(input, 0, picture1.ContentLength); command.Parameters.Add("@image1Name", SqlDbType.VarChar, 100).Value=picture1.FileName; command.Parameters.Add("@image1Byte", SqlDbType.VarBinary, file.Length).Value=input;
This is how I upload files all over the place. It's much faster and less hastle to clean up as the GC will bin the file when you're finished with it.
I hope that helps ^_^
Addendum to last solution:
Ok - first you need to try to delete the item, preferably before the reference, but it doesn't matter. Just make sure you get the path before deleting the entry:
protected void delete_rOW(object sender, GridViewDeleteEventArgs e) { //I'll write this "best practice" var id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text) string selectCmd = "select path from pikiphones WHERE id= @id" using(SqlCommand com = con.CreateCommand){ com.Text = selectCmd; com.Parameters.Add(new SqlParameter("@id", SqlDbType.Int){Value=id}); var path = com.ExecuteScalar().toString(); if(!string.isNullOrEmpty(path) && Path.Exists(path)){ //try catch? Path.Delete(path); } } String deleteCmd = "DELETE FROM pikiphones WHERE id= @id"; SqlCommand myCommand = new SqlCommand(deleteCmd, con); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)); myCommand.Parameters["@id"].Value = id; try { myCommand.ExecuteNonQuery(); } catch (SqlException) { Label1.Text = "ERROR: Could not delete record"; } myCommand.Connection.Close(); BindGrid(); }
You may have to debug it. I know I have some method keywords wrong. Let me know if you get stuck ^_^
这篇关于使用ASP.NET C#中的gridview从文件夹和数据库中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!