I have a trouble with the following code. The program seems to stuck sometimes (apparently no pattern…often in the second query since the form containing this code run but not always) for 5-20 seconds in the row
Dim rdr As SQLiteDataReader = cmd.ExecuteReader
If the code doesn’t stuck in second query since form started, it won’t never stuck. If I close and reopen the form I have the same 50-60% chance to have same error mostly on 2nd query.
Then it always restart from this point sometimes with no error…just big delay of 5-20 seconds and sometimes with error “The database file is locked, database is locked” at line
cmd.ExecuteNonQuery()
(2° time..not inside if block)
This error will forbid the user to save its change and sometimes corrupt the data record and, of course, introduces a huge delay that I can’t accept 🙁
Any Idea of what I’m doing wrong?
I’m developing with visual studio 2008 for a windows CE 6.0 device with 3.5 framework and
ADO.NET 2.0/3.5 SQLite Data Provider
Version 1.0.66.0 April 18, 2010
Using SQLite 3.6.23.1
http://sqlite.phxsoftware.com/
Thank you
'Update current program on database
Public Sub AggiornaUserOnDatabase(ByVal name As String, ByVal password As String, ByVal permission As String)
'No spaces
name = Trim(name)
'Database var
Dim ConString As String = "Data Source = " + DatabaseAccountPath + ";"
Dim con As New SQLiteConnection
Dim cmd As SQLiteCommand
'Read from database
Try
con.ConnectionString = ConString
con.Open()
'Check if row is existing
cmd = con.CreateCommand
cmd.CommandText = "SELECT " + DBACCNAME + " FROM " + DatabaseAccountTableName + " WHERE " + DBACCNAME + " = '" + name + "'"
Dim rdr As SQLiteDataReader = cmd.ExecuteReader
'If empty...create the record
If Not rdr.Read Then
cmd = con.CreateCommand
cmd.CommandText = "INSERT INTO " + DatabaseAccountTableName + " (" + DBACCNAME + ") VALUES ('" + name + "')"
cmd.ExecuteNonQuery()
End If
rdr.close()
'Update the values
cmd = con.CreateCommand
cmd.CommandText = "UPDATE " + DatabaseAccountTableName + " SET " _
+ DBACCPASSWORD + " = '" + password + "', " _
+ DBACCPERMISSION + " = '" + permission + "' " _
+ "WHERE " + DBACCNAME + " = '" + name + "'"
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
Catch ex As Exception
AggiungiRigaSuFile(ErroriPath, "Editing users account database: " + ex.Message, Configurazione.DiagnosticaOff, True)
End Try
End Sub
You are using the cmd when it has an open reader. You need to close the reader before you use the cmd again. And don’t even use a reader. Do a select count(*) to to see if the record exists.