问题描述
大家早上好。
我正在从Access数据库(.accdb)读取附件(JPEG文件)。
用户从组合框中选择一个驱动程序,程序然后去获取驱动程序许可证图像。当我尝试删除保存的文件名时,用户第二次选择给定的驱动程序名称,例程崩溃,文件被另一个进程使用。 (参见代码中的标记)
Good morning everyone.
I am reading an attachment (JPEG file) from an Access database (.accdb).
The user selects a Driver from a combobox, the program then goes to get the drivers license image. The second time that the user selects a given drivers name the routine crashes, with "File is being used by another process" when I try to delete the saved filename. (See Marker in code)
Private Sub GetLicenceImages(ByVal theDriver As String, theLicence As String)
Dim engine As New Dao.DBEngine
Dim database As Dao.Database = engine.OpenDatabase(CommonDBPath, False, False, "MS Access;PWD=xxxxxxx")
Dim rs As Dao.Recordset = database.OpenRecordset("Select HRL_Licence_Image From HR_Licences WHERE HRL_Name_Surname = '" & theDriver & "' AND HRL_Licence_Type = '" & theLicence & "'")
While Not rs.EOF
Dim rs1 As Dao.Recordset2 = CType(rs.Fields("HRL_Licence_Image").Value, Recordset2)
While Not rs1.EOF
DriversLicencePicPath = LocalTempPath & rs1("FileName").Value.ToString
Dim fld As Dao.Field2 = CType(rs1("FileData"), Field2)
System.IO.File.Delete(DriversLicencePicPath) '<===== CRASH OCCURS HERE
fld.SaveToFile(DriversLicencePicPath)
'--------------------------------
Dim img As Image = Image.FromFile(DriversLicencePicPath)
but_Driver_Licence.BackgroundImage = img
but_Driver_Licence.Text = ""
but_Delete_Drivers_Pic.Visible = True
'--------------------------------
rs1.MoveNext()
End While
rs.MoveNext()
End While
rs.Close()
End Sub
使用该文件的过程就是这个程序,我不知道要关闭或处理什么(或其他)以显示图像第二次。
非常感谢任何建议或指导。
问候和谢谢。 />
我尝试了什么:
我已从but_Driv中删除了图片输入此例程之前的er_Licence.BackgroundImage按钮。
不知道还能做什么。
The process that is using the file is this program and I have no idea what to Close or Dispose (or other) in order to show the image the second time.
Any advice or guidance would be much appreciated.
Regards and thanks.
What I have tried:
I have removed the image from the but_Driver_Licence.BackgroundImage button before entering this routine.
Don't know what else to do.
推荐答案
我在输入此例程之前已从but_Driver_Licence.BackgroundImage按钮中删除了图像。
I have removed the image from the but_Driver_Licence.BackgroundImage button before entering this routine.
这并不意味着文件被释放。如果您使用Image.FromFile从文件加载图像,那么文档非常清楚: []
That doesn't mean that the file is freed. If you loaded the image from the file using Image.FromFile, then the documentation is pretty clear: Image.FromFile Method (String, Boolean) (System.Drawing)[^]
文件保持锁定,直到图像被丢弃。
The file remains locked until the Image is disposed.
这意味着只要你的代码没有在Image类实例本身显式调用Dispose,就无法写入,移动或删除文件。
当您从按钮中删除它时,请将其处理 - 或者将文件加载到图像中时更好,将其复制到新的位图并处理原始图像以使其锁定的周期尽可能短。 />
并且不要像这样进行数据库访问:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
Which means as long as your code has not explicitly called Dispose on the Image class instance itself, the file cannot be written, moved or deleted.
When you remove it from the Button, Dispose it - or better when you load teh file into an image, copy it to a new bitmap and Dispose the original so that teh period it is locked is as short as possible.
And don't do database access like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
这篇关于如何确定“其他进程使用的文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!