我遇到了两个问题[注意:这是我第一次使用Visual Studio使用SQL进行编码]
我正在使用SQLite Server Compact工具箱和Nuget System.Data.SQLite -Version 1.0.106
每次我试图保存它时都会向我发送此错误
System.Data.SQLite.SQLiteException:数据库被锁定
我正在尝试创建更新和删除按钮
这是我的更新按钮
Private Sub Bttn_Update_Click(sender As Object, e As EventArgs) Handles Update_Bttn.Click
Dim cons = New SQLiteConnection("uri=file:C:\Sqlite\Klinik_NurzawatiDB.db; version=3;Pooling=True;Max Pool Size=100;")
cons.Open()
If TextBox2.Text = "" Then
MsgBox("Please insert a file Series", vbCritical, "Cannot update: Missing")
ElseIf TextBox5.Text = "" Then
MsgBox("Please insert file Group", vbInformation, "Required")
TextBox5.Select()
Else
Try
Dim sql As String = "UPDATE ClassTbl SET Series='" & TextBox2.Text & "', SubSeries='" & TextBox3.Text & "', Groups='" & TextBox5.Text & "', SubGroup='" & TextBox4.Text & "', ReferenceCode='" & TextBox6.Text & "',Seperator='" & TextBox7.Text & "', ItemName='" & TextBox1.Text & "' WHERE ID ='" & TextBox9.Text & "'"
Dim cmd As SQLiteCommand = New SQLiteCommand(sql, cons)
cmd.ExecuteNonQuery()
MsgBox("Successfully updated!", MsgBoxStyle.Information, "File Status")
'^OK
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
这是我的删除按钮
Private Sub Delete_Bttn_Click(sender As Object, e As EventArgs) Handles Delete_Bttn.Click
If TextBox9.Text = "" Then
MsgBox("Please select Id to delete", vbExclamation, "File not selected")
Else
Dim con As New SQLiteConnection("uri=file:c:\sqlite\Klinik_NurzawatiDB.db; Version=3")
con.Open()
Dim response As DialogResult =
MessageBox.Show(
"Are you sure you want to delete this row?",
"Records Lite: Confirm delete?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2)
If (response = DialogResult.No) Then
'e.Cancel = True
Exit Sub
TextBox2.Text = ""
End If
Dim sql As String
sql = "Delete From ClassTbl WHERE ID like '" & TextBox9.Text & "'"
Dim cmd As SQLiteCommand = New SQLiteCommand(sql, con)
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Successfully", vbInformation, "Delete status")
End If
End Sub
我总是在
遇到问题cmd.ExecuteNonQuery()
对于这两个按钮,任何解决方案?我已经创建了相同的数据库,但仍然遇到了这些问题
答案 0 :(得分:3)
您的代码存在的问题是您没有在更新方法中关闭连接。您需要添加cons.Close()
。无论如何,在实现Using
的所有类中使用IDisposable总是一个好主意