当用户单击删除按钮时,如何从服务器和数据库中删除图像? [英] How can I make images to be deleted from server and database when user clicks the delete button?

查看:76
本文介绍了当用户单击删除按钮时,如何从服务器和数据库中删除图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击删除按钮时从服务器和数据库中删除图像。



我的代码是: -



 受保护  void  GridView1_RowDeleting ( object  sender,GridViewDeleteEventArgs e)
{
string str = @ data source = DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog = new; integrated security = true;
SqlConnection con = new SqlConnection(str);
con.Open();
string s = GridView1.DataKeys [e.RowIndex] .Value.ToString();

SqlCommand cmd = new SqlCommand( 从date1删除,其中ddate =' + s + ', CON);
cmd.ExecuteNonQuery();
con.Close();


Response.Write( 已成功删除);
}

解决方案

如果将实际图像数据存储在数据库的date1表中,那么该代码将被删除图像数据。如果将其存储在另一个表中,则需要启用级联删除 [ ^ ]或编写代码以手动将其从其他表中删除。 />


如果将它存储在磁盘文件中并存储数据库中文件的路径,那么最好使用 .File.Delete [ ^ ]作为您的webserver几乎肯定可以访问保存图像数据的文件夹,而SQL服务器计算机可能不会。




用于从数据库中删除图像ÿ你可以写一个删除查询

并且从服务器删除图像你可以使用后续代码



 < span class =code-keyword> string  file_name = GridView1.DataKeys [e.RowIndex] .Value.ToString();  //  您的图片名称 
string path = Server.MapPath( YourImageFolderName // + file_name);
FileInfo file = new FileInfo(path);
if (file.Exists) // 检查文件exsit是否
{
file.Delete();
}
其他
{
// < span class =code-comment>显示消息此文件不存在
}





感谢


I want the images to be deleted from server as well as from database when user clicks the delete button.

My Code is:-

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
        SqlConnection con = new SqlConnection(str);
        con.Open();
        string s = GridView1.DataKeys[e.RowIndex].Value.ToString();

        SqlCommand cmd = new SqlCommand("Delete from date1 where ddate='" + s + "'",con);
        cmd.ExecuteNonQuery();
        con.Close();


        Response.Write("Deleted Sucessfully");
    }

解决方案

If you store the actual image data in your database, in the date1 table, then that code will delete the image data. If you store it in a different table, then you will need to enable cascade deletes[^] or write teh code to manually remove them from the other table as well.

If you store it in a disk file and store a path to the file in your DB, then you are best off deleting that from your C# code using File.Delete[^] as your webserver will almost certainly have access to the folder holding the image data, and the SQL server computer probably won't.


Hi,
For deleting image from database you can write a delete query
And for deleting image from server you can use folling code

string file_name = GridView1.DataKeys[e.RowIndex].Value.ToString(); //your Image name
  string path = Server.MapPath("YourImageFolderName//" + file_name);
  FileInfo file = new FileInfo(path);
  if (file.Exists)//check file exsit or not
  {
       file.Delete();
  }
  else
  {
      //Show msg "This file does not exists "
  }



Thanks


这篇关于当用户单击删除按钮时,如何从服务器和数据库中删除图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆