我的數據表在兩個地方,一個DataGridView和組合框裝 組合框來選擇要修改的記錄(一個文本框輸入一個新值) 而在DataGridView是看改變(我放棄了(現在)直接從DataGridView中更新)VB.NET SQL數據庫鎖定
Private Sub EditLoc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
con.Open()
Dim sql = Nothing
sql = "SELECT Location FROM Location"
Dim cmdDataGrid As SQLiteCommand = New SQLiteCommand(sql, con)
Dim da As New SQLiteDataAdapter
da.SelectCommand = cmdDataGrid
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
Dim readerDataGrid As SQLiteDataReader = cmdDataGrid.ExecuteReader()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try ' TRY CATCH for combobox
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Location FROM Location"
dr = cmd.ExecuteReader()
' Fill a combo box with the datareader
Do While dr.Read = True
ComboBox1.Items.Add(dr.GetString(0))
Loop
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0 ' The first item has index 0 '
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
這工作完全 Picture
問題是,當我點擊保存,應用程序掛起了一會兒,然後我得到的「數據庫已鎖定」錯誤picture
這裏是保存按鈕的代碼:
Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click
Try
con.Open()
cmd = con.CreateCommand
cmd.CommandText = "UPDATE Location set Location = '" & TextBox1.Text & "' WHERE Location = '" & ComboBox1.Text & "'"
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
感謝您的幫助
* Everythjing *具有'的Dispose()'方法應予以處置後調用拉();始終使用SQL參數;並且不使用全局提供者對象。 – Plutonix
感謝您的回覆,請您詳細說明一下嗎? – user7406533