从数据库中删除包含该图像的记录后,从文件夹中删除图像 [英] delete image from folder after deleting the record containing that image from databse

查看:158
本文介绍了从数据库中删除包含该图像的记录后,从文件夹中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我的数据库中有一个包含两列ID和Image的表。

将我的图像插入数据库时​​使用以下代码。

hello

I have a table in my database containing two columns ID and Image.
when inserting my image into database i use the following code.

SqlCommand cmd = new SqlCommand("INSERT INTO info(ID,Img_Image) VALUES(@ID,@Img_image))

cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Image", test_image);

...





test_image从路径加载图像即(C:\ foldername)。



现在我的问题是当我删除一个特定的记录时...文件夹中相应的图像应该被删除。



PS:我使用标准的SQL删除用于删除记录的代码。



That test_image loads the Image from a path i.e. (C:\foldername).

Now my problem is that when i delete a specific record...the corresponding Image in the folder should get deleted.

P.S.: i am using the standard SQL delete code for deleting a record.

推荐答案

你要清楚:

1)你把你的图像放在文件夹中

2)你插入带有一些元数据的记录图像二进制数据将图像保存在文件夹中

3)删除记录时要从文件夹中删除图像?

正确吗?

问题:

- 为什么要保留图像数据的副本?

- 为什么你把它存放到数据库后不要从文件夹中删除吗?

- 你到目前为止考虑了什么?



其实你有几种选择:

A)使用 FILESTREAM功能 [ ^ ],你获得了很多:没有重复,快速访问,你有记录和文件在fil esystem和它们自动链接

B)从SQL Server使用OLE自动化。请参阅: http://www.kodyaz。 com / articles / delete-file-from-sql-server-xp-cmdshell-ole-automation-procedures.aspx [ ^ ]和 http://kb.hyve.com/wiki/DeleteFilesInSQLServer [ ^ ]

C)制作 CLR程序 [ ^ ]为此目的

D)让消费者/客户负责存储要删除的文件。谁告诉你,你需要在sql server中做到这一点?



就个人而言,我会考虑替代A!这是一个很棒的功能!
Yous to be clear:
1) You put your image in the folder
2) You insert the record with some metadata and the image binary data and you keep the image in the folder
3) When you delete the record you want to delete the image from the folder too?
Correct?
Questions:
- Why do you keep duplicate of the image data?
- Why don''t you delete from the folder after you stored it in the database?
- What have you considered so far?

Actually you have several alternatives:
A) use FILESTREAM feature[^], and you gain a lot: no duplication, quick access, you have the record and the file on the filesystem and they are automatically linked
B) use OLE Automation from SQL Server. See: http://www.kodyaz.com/articles/delete-file-from-sql-server-xp-cmdshell-ole-automation-procedures.aspx[^] and http://kb.hyve.com/wiki/DeleteFilesInSQLServer[^]
C) Make a CLR procedure[^] for this purpose
D) Make the "consumer/client" that is responsible for storing the file to delete on it''s behalf. Who told you that you need to do that from within sql server?

Personally I would consider alternative A! It is a great feature!


从数据库中成功删除行后,执行以下代码。



After you have successfully deleted the row from the database, execute the following code.

try {
        File.Delete(@"C:\foldername\" + imagefilenamegoeshere);
    }
catch  (IOException e)
    {
    }


这篇关于从数据库中删除包含该图像的记录后,从文件夹中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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