2013-03-30 75 views
1

我的數據庫中有一個包含兩列ID和圖像的表。 當我的圖像插入數據庫時​​,我使用下面的代碼。從數據庫中刪除包含該圖像的記錄後從文件夾中刪除圖像

<pre lang='cs'> 

     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); 
    </pre> 

... 即test_image從路徑加載圖像即(C:\文件夾)。

現在我的問題是,當我刪除一個特定的記錄...該文件夾中的相應圖像應該被刪除。

P.S .:我使用標準的SQL刪除代碼來刪除記錄。

+0

' test_image'包含路徑或一些圖像數據? – didierc

回答

3

從表中刪除記錄後,您可以通過使用System.IO.File.Delete或使用的FileInfo

string filePath = "C:\test.txt"; 
FileInfo file = new FileInfo(filePath); 
if (file.Exists) 
{ 
    file.Delete(); 
} 
+0

我只是添加它最好使用 string filePath = Server.MapPath(「C:\ test.txt」); – Kadaj

0

刪除物理文件刪除物理文件 - 工程100%

File.Delete(Server.MapPath("../App_Themes/UPLOADS/banner_img/5.jpg")); 

//if some error occurs like , don't have rights 

//uncheck readonly option in folder properties