SQLite - > bindingsource,写回数据

时间:2018-02-07 07:06:47

标签: vb.net sqlite bindingsource

以下代码确实在dataAdapter_AT.Update(dT)行上产生系统无效操作异常。

我一直在四处搜索,但是一旦我在datagridview中修改数据,就无法弄清楚如何将数据写回SQLite数据库。

Dim bs_ATDB_database As New BindingSource
Dim dataAdapter_AT As New SQLiteDataAdapter
Dim dttable_AT As New DataTable


Private Sub BT_ATDB_reload_Click(sender As Object, e As EventArgs) Handles BT_ATDB_reload.Click

    loadtranslationdatabase()

End Sub

Sub loadtranslationdatabase()
    Dim SQLconnect_AT As New SQLite.SQLiteConnection()
    Dim SQLcommand_AT As SQLiteCommand

    SQLconnect_AT.ConnectionString = "Data Source=" & System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString) & "\database_AT.db3;"
    SQLconnect_AT.Open()
    SQLcommand_AT = SQLconnect_AT.CreateCommand

    dataAdapter_AT = New SQLiteDataAdapter("SELECT * FROM translations order by stringid asc;", SQLconnect_AT)

    dataAdapter_AT.Fill(dttable_AT)

    bs_ATDB_database.DataSource = dttable_AT

    DGV_ATDB_Database.DataSource = bs_ATDB_database

End Sub


Private Sub DGV_ATDB_Database_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_ATDB_Database.CellEndEdit
    bs_ATDB_database.EndEdit()

    Dim dt As DataTable = CType(bs_ATDB_database.DataSource, DataTable)
    dataAdapter_AT.Update(dt)

End Sub

有人可以解释我遗漏的内容以及为什么没有使用编辑更新数据库吗?

由于

1 个答案:

答案 0 :(得分:0)

终于解决了这个问题:

Dim bs_ATDB_database As New BindingSource
Dim dataAdapter_AT As New SQLiteDataAdapter
Dim dttable_AT As New DataTable


Private Sub BT_ATDB_reload_Click(sender As Object, e As EventArgs) Handles BT_ATDB_reload.Click

loadtranslationdatabase()

End Sub

Sub loadtranslationdatabase()
Dim SQLconnect_AT As New SQLite.SQLiteConnection()


SQLconnect_AT.ConnectionString = "Data Source=" & System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString) & "\database_AT.db3;"
SQLconnect_AT.Open()

dataAdapter_AT = New SQLiteDataAdapter("SELECT rowID, * FROM translations order by stringid asc;", SQLconnect_AT)

dataAdapter_AT.Fill(dttable_AT)

bs_ATDB_database.DataSource = dttable_AT

DGV_ATDB_Database.DataSource = bs_ATDB_database

End Sub


Private Sub DGV_ATDB_Database_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_ATDB_Database.CellEndEdit
bs_ATDB_database.EndEdit()

Dim dt As DataTable = CType(bs_ATDB_database.DataSource, DataTable)
  dataAdapter_AT.UpdateCommand = New SQLiteCommandBuilder(dataAdapter_AT).GetUpdateCommand()
dataAdapter_AT.Update(dt)

End Sub

在selct语句中添加了RowID,并在更新命令之前添加了dataAdapter_AT.UpdateCommand = New SQLiteCommandBuilder(dataAdapter_AT).GetUpdateCommand()