VB.Net和SQLite无法保存

时间:2019-01-21 15:35:37

标签: vb.net sqlite

如标题所示,它不保存

代码

Try
        connection()
        Dim cmd As New SQLiteCommand
        'cmd.Connection = connection()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "UPDATE Emp SET Value = Value +1 WHERE Id='1'"
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        connect.Close()
        MsgBox("Data Has been Saved !")
    Catch ex As Exception
        MsgBox("Failed when saving data")
    End Try

如果Id等于X,则基本上以1对数递增Log。

错误似乎在“ cmd.ExecuteNonQuery()”中

enter image description here

表名:Emp 值=整数 ID =整数

2 个答案:

答案 0 :(得分:1)

在线发表评论和解释。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try
        'Get your connection locally
        Using cn As New SQLiteConnection("Your connection string")
            'The using blocks ensure that your database objects are 
            'closed and disposed even if there is an error.
            'You have put your Id in single quotes '1'
            'This indicates that it is a string
            'Usually an Id is a number, check your database
            Using cmd As New SQLiteCommand("UPDATE Emp SET Value = Value +1 WHERE Id='1'", cn)
                'You can pass your command text and the connection
                'directly to the command constructor
                cmd.CommandType = CommandType.Text
                cn.Open()
                cmd.ExecuteNonQuery()
            End Using
        End Using
        MsgBox("Data Has been Saved !")
    Catch ex As Exception
        MsgBox("Failed when saving data. " & ex.Message)
    End Try
End Sub

如果Where子句中的ID不是文字且来自用户输入,则需要使用参数。

答案 1 :(得分:0)

我不知道这是bug还是什么...但是我创建了另一个项目,然后复制并粘贴所有内容。

奇怪的事情是可行的!但原始版本仍然没有。我的意思是我复制并粘贴了从设计器到代码以及复制工作者的所有内容,而原始文件则没有

我正在使用VS 2013 Updated 5 ...

谢谢你们。