0

我对以下代码有疑问。该程序似乎有时会卡住(显然没有模式......通常在第二个查询中,因为包含此代码的表单运行但并非总是如此)连续 5-20 秒

Dim rdr As SQLiteDataReader = cmd.ExecuteReader

如果代码在表单启动后没有卡在第二个查询中,它就永远不会卡住。如果我关闭并重新打开表单,我有相同的 50-60% 的机会在第二次查询中出现相同的错误。

然后它总是从这一点重新启动,有时没有错误......只是5-20秒的大延迟,有时在行出现错误“数据库文件已锁定,数据库已锁定”

cmd.ExecuteNonQuery() (2°时间..不在 if 块内)

此错误将禁止用户保存其更改并且有时会损坏数据记录,当然,还会引入我无法接受的巨大延迟:(

知道我做错了什么吗?

我正在使用 Visual Studio 2008 为具有 3.5 框架的 Windows CE 6.0 设备和

ADO.NET 2.0/3.5 SQLite 数据提供程序版本 1.0.66.0 2010 年 4 月 18 日 使用 SQLite 3.6.23.1 http://sqlite.phxsoftware.com/

谢谢

'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
4

1 回答 1

2

当它有一个开放的阅读器时,您正在使用 cmd。您需要在再次使用 cmd 之前关闭阅读器。甚至不要使用阅读器。执行 select count(*) 以查看记录是否存在。

cmd.CommandText = "SELECT count(*) FROM " + ...
int rowRet = (Int32)sqlCmd2.ExecuteScalar();

if (rowCount == 0)
{
    // do NOT create the command gain just use it
    cmd.CommandText = "INSERT INTO " + DatabaseAccountTableName ...
    // insert all the values here so the update is not required
    cmd.ExecuteNonQuery();
}
else if (rowCount == 1)
{
    cmd.CommandText = "UPDATE " + DatabaseAccountTableName + " SET " _
                    + DBACCPASSWORD + " = '" + password + "', " _
                    + DBACCPERMISSION + " = '" + permission + "' " _
                    + "WHERE " + DBACCNAME + " = '" + name + "'"
    cmd.ExecuteNonQuery();
}
else 
{
     // throw an error expected 0 or 1
}
于 2012-09-22T12:46:17.730 回答