2017-03-21 88 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