Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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);

...


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.
Posted
Comments
[no name] 30-Mar-13 8:09am    
And you question is what? How is deleting a file a problem?
Member 8657300 30-Mar-13 8:18am    
yes
i have a collection of images in a folder and then i insert them into the database. i cannot delete the image after inserting it. i need the image in the folder after insertion. what i want to know can i simultaneously delete the image from the folder when i delete its record from the database.
Member 8657300 30-Mar-13 8:17am    
yes what you have mentioned is correct.
i have a collection of images in a folder and then i insert them into the database.
i cannot delete the image after inserting it. i need the image in the folder after insertion.
what i want to know can i delete the image from the folder when i delete its record from the database.
Zoltán Zörgő 30-Mar-13 8:29am    
See my answer. Still, consider my questions for think-trough. In general it smells like bad design to have duplicate data, and especially for large objects like images. Storing such large objects in the database itself might be also an overhead. This is why I am suggesting the FILESTREAM feature. You can find good tutorials here and out there too. You need only a few configuration steps and you have it, and you don't need to rewrite your code, only a few small modifications are necessary.

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!
 
Share this answer
 
v3
After you have successfully deleted the row from the database, execute the following code.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900