【问题标题】:VB.NET SQL Database lockedVB.NET SQL 数据库锁定
【发布时间】:2017-03-21 21:04:36
【问题描述】:

我的数据表在 2 个地方加载,一个 DataGridView 和一个 ComboBox ComboBox是选择要编辑的记录(一个TextBox输入新值) 而DataGridView是看变化的(我放弃了(暂时)直接从DataGridView更新)

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 参数;并且不要使用全局提供者对象。
  • 谢谢您的回复,能详细点吗?

标签: vb.net sqlite


【解决方案1】:

我通过从代码中删除所有“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

在表单加载时我有

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

Pull 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

现在一切正常,没有错误!

保存后调用 pull() 会更新 DataGridView

感谢您的帮助

【讨论】:

  • 除了完全正确配置的 DataAdapter 之外,不应重用 DB 提供程序对象(DBCommand、DBReader、DBConnection)。 ComboBox 也可以使用 DataSource 而不是将数据复制到项目集合。这个答案是次优的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 2017-07-04
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多