SQLite keeps the database locked even after the connection is closed

后端 未结 12 1298
故里飘歌
故里飘歌 2020-11-30 02:33

I\'m using System.Data.SQLite provider in an ASP.NET application (framework 4.0). The issue I\'m running into is that when I INSERT something in a table in the SQLite databa

12条回答
  •  -上瘾入骨i
    2020-11-30 02:51

    This was one of the top google results I had found when I ran into this error. However, none of the responses helped me so after more searching around and googling I came up with this code that works from some of the code from http://www.tsjensen.com/blog/post/2012/11/10/SQLite-on-Visual-Studio-with-NuGet-and-Easy-Instructions.aspx

    However, I did not have to use the NuGet at all. What my program does is downloads a db file from a server every time it is opened. Then if a user updates that db, it will be uploaded for everyone to get the next time they open the same program. I was getting the error that the file was in use after updating the local file and trying to upload it to our SharePoint. Now it works fine.

    Public Function sqLiteGetDataTable(sql As String) As DataTable
        Dim dt As New DataTable()
        Using cnn = New SQLiteConnection(dbConnection)
            cnn.Open()
            Using cmd As SQLiteCommand = cnn.CreateCommand()
                cmd.CommandText = sql
                Using reader As System.Data.SQLite.SQLiteDataReader = cmd.ExecuteReader()
                    dt.Load(reader)
                    reader.Dispose()
                End Using
                cmd.Dispose()
            End Using
            If cnn.State <> System.Data.ConnectionState.Closed Then
                cnn.Close()
            End If
            cnn.Dispose()
        End Using
        Return dt
    End Function
    

提交回复
热议问题