Skip to content

Instantly share code, notes, and snippets.

@edymtt
Created October 1, 2012 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edymtt/3813867 to your computer and use it in GitHub Desktop.
Save edymtt/3813867 to your computer and use it in GitHub Desktop.
System.Data.SQLite -- How to correctly dispose a table adapter to avoid file locking after connection close
'In Table Adapter
Protected Overrides Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
Common.DisposeTableAdapter(disposing, _
_adapter, _
_commandCollection)
End Sub
Module Common
''' <summary>
''' Disposes a TableAdapter generated by SQLite Designer
''' </summary>
''' <param name="disposing"></param>
''' <param name="adapter"></param>
''' <param name="commandCollection"></param>
''' <remarks>You must dispose all the command, otherwise the file remains locked and cannot be accessed (for example, to read or delete it)</remarks>
Sub DisposeTableAdapter(disposing As Boolean, _
adapter As System.Data.SQLite.SQLiteDataAdapter, _
commandCollection As IEnumerable(Of System.Data.SQLite.SQLiteCommand))
If disposing Then
DisposeSQLiteTableAdapter(adapter)
For Each currentCommand In commandCollection
currentCommand.Dispose()
Next
End If
End Sub
Sub DisposeSQLiteTableAdapter(adapter As System.Data.SQLite.SQLiteDataAdapter)
If adapter IsNot Nothing Then
DisposeSQLiteTableAdapterCommands(adapter)
adapter.Dispose()
End If
End Sub
Sub DisposeSQLiteTableAdapterCommands(adapter As System.Data.SQLite.SQLiteDataAdapter)
For Each currentCommand In {adapter.UpdateCommand, _
adapter.InsertCommand, _
adapter.DeleteCommand, _
adapter.SelectCommand}
If currentCommand IsNot Nothing Then
currentCommand.Dispose()
End If
Next
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment