2017-03-21 89 views
0

我的數據表在兩個地方,一個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 

感謝您的幫助

+0

* Everythjing *具有'的Dispose()'方法應予以處置後調用拉();始終使用SQL參數;並且不使用全局提供者對象。 – Plutonix

+0

感謝您的回覆,請您詳細說明一下嗎? – user7406533

回答

0

我卸下所有 「cmd.Dispose()」 解決了這個問題, 「da.Dispose()」和 「con.Close()」 從代碼,使他們只能在

Private Sub Closebtn_Click(sender As Object, e As EventArgs) Handles Closebtn.Click 
    da.Dispose() 
    cmd.Dispose() 
    con.Close() 
    Dim fems As New EMS 
    fems.Show() 
    Me.Close() 
End Sub 

在窗體的Load我

Private Sub EditLoc_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    con.Open() 
    Call pull() 
End Sub 

和拉子擁有所有休息...

Private Sub pull() 
    Try 
     Dim sql = Nothing 
     sql = "SELECT Location FROM Location" 
     Dim cmdDataGrid As SQLiteCommand = New SQLiteCommand(sql, con) 
     da.SelectCommand = cmdDataGrid 
     Dim dt As New DataTable 
     da.Fill(dt) 
     DataGridView1.DataSource = dt 
     Dim readerDataGrid As SQLiteDataReader = cmdDataGrid.ExecuteReader() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

    Try ' TRY CATCH for combobox 
     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 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 

這裏是我的保存按鈕

Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click 

    If Not TextBox1.Text = Nothing Then 
     Try 
      cmd = con.CreateCommand 
      cmd.CommandText = "UPDATE Location set Location = '" & TextBox1.Text & "' WHERE Location = '" & ComboBox1.Text & "'" 
      cmd.ExecuteNonQuery() 

     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 

     Call pull() 
     TextBox1.Text = Nothing 

    End If 

End Sub 

現在一切正常,沒有錯誤!

節省將更新的DataGridView

感謝您的幫助

+0

除了完全配置正確的DataAdapter外,DB提供者對象不應該被重用(DBCommand,DBReader,DBConnection)。 ComboBox也可以使用DataSource而不是將數據複製到items集合。這個答案是次優的 – Plutonix