vb.net 使用 SqlConnection 不会释放文件的使用

vb.net Using SqlConnection doesn't free the file from use

我正在使用本地 .mdf 文件并对数据库执行一些查询,我正在使用 USING 块来确保正确处理 SqlConnection 和 SqlReader。

然后我尝试读取文件的数据以生成文件的 MD5 哈希,但它说文件仍在使用中。

代码不是最干净的,这是我第一次在 VB.NET 应用程序中使用 Sql。

SQL插入:

Dim finalW As String = ""
Dim finalO() As String
Dim currentcounter As Integer = 0
For Each Dir As String In System.IO.Directory.GetDirectories(Pathfinder)
    Dim dirInfo As New System.IO.DirectoryInfo(Dir)
    Dim temp As New List(Of String)
    For Each currentFile In Directory.GetFiles(Pathfinder & "\" & dirInfo.Name & "\", "*.png", SearchOption.TopDirectoryOnly)
        temp.Add(Path.GetFileName(currentFile))
    Next
    If temp.Count <> 0 Then
        finalW = temp.Find(AddressOf GetNewIcon)
        finalO = temp.FindAll(AddressOf GetOldIcon).ToArray
        If finalW <> "" Then
            Using con As New SqlClient.SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""" & PathFinal & "ImaginiDB.mdf"";Integrated Security=True")
                con.Open()
                Using cmd = con.CreateCommand()
                    cmd.CommandType = CommandType.Text
                    cmd.CommandText = "INSERT NewIcon (Name) VALUES ('" & finalW.Trim() & "')"
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
                End Using
                currentcounter = currentcounter + 1
                Dim Id As String = ""
                Using command = con.CreateCommand()
                    command.CommandType = CommandType.Text
                    command.CommandText = "SELECT * FROM NewIcon WHERE Name='" & finalW & "'"
                    Using reader As SqlDataReader = command.ExecuteReader()
                        While reader.Read()
                            Id = reader(0)
                        End While
                    End Using
                End Using
                For Each item As String In finalO
                    Using cmd2 = con.CreateCommand()
                        cmd2.CommandType = CommandType.Text
                        cmd2.CommandText = "INSERT OldIcon (NID,Name) VALUES ('" & Id & "','" & item.ToString.Trim() & "')"
                        cmd2.Connection = con
                        cmd2.ExecuteNonQuery()
                    End Using
                    currentcounter = currentcounter + 1
                Next
                Dim cur As Long = currentcounter * 100 / counter
                SetProgress(cur)
            End Using
        End If
    End If
Next
GC.Collect()
GC.WaitForPendingFinalizers()
SetLabel4Text("FINISHED IMPORT", Color.Red)

MD5 生成 运行 此过程完成后:

Public Function GenMD5(ByVal Filename As String) As String
    Dim MD5 = System.Security.Cryptography.MD5.Create
    Dim Hash As Byte()
    Dim sb As New System.Text.StringBuilder
    Using st As New IO.FileStream(Filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        Hash = MD5.ComputeHash(st)
    End Using
    For Each b In Hash
        sb.Append(b.ToString("X2"))
    Next
    Return sb.ToString
End Function

正如@jmcilhinney 在评论中提到的那样 正如 MSDN 所说,不同的连接使用不同的池:

When a connection is first opened, a connection pool is created based on an exact matching algorithm that associates the pool with the connection string in the connection.

于是我决定实现方法:

SqlConnection.ClearPool(connection As SqlConnection)

我把这个放在我的结束使用之前:

    Dim cur As Long = currentcounter * 100 / counter
    SetProgress(cur)
    SqlConnection.ClearPool(con)
End Using