如何从 Visual Basic 2013 .net 中的 DataGridView 更新 SQLite 数据库?
How to update SQLite database from DataGridView in Visual Basic 2013 .net?
我正在将来自 SQLite table 的数据显示到 DataGridView 中,如下所示 -
Private Sub Subjects_Manager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New SQLiteConnection("Data Source = c:\demo\test.db;Version=3;")
con.Open()
sql = "SELECT * FROM med_subjects"
da = New SQLiteDataAdapter(sql, con)
da.Fill(ds, "SubjectsList")
DataGridView1.DataSource = ds.Tables("SubjectsList").DefaultView
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Subject Id"
.Columns(1).HeaderCell.Value = "Subject Name"
End With
DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
con.close()
End Sub
我想将在 DataGridView 中完成的更改(Row/s 的更新或 Row/s 的插入)保存回 SQLite table。但是我找不到办法。
编辑 1 :
我知道 SQLite 的 Insert/Update 查询,但我不知道如何以及在何处保存它们,以便可以触发它们以响应 DataGridView 中所做的更改。例如
' I am using this variable for demonstration, in reality InsertSubjectSqlString will be equal to changes done in DataGridView
Dim InsertSubjectSqlString As String = "Insert into med_subjects (Subject_Name) Values ('_Miscellaneous')"
Dim SqliteInsertRow As SQLiteCommand
SqliteInsertRow = con.CreateCommand
SqliteInsertRow.CommandText = InsertSubjectSqlString
SqliteInsertRow.ExecuteNonQuery()
但是我不知道,我应该把它放在哪里?
编辑 2:
看到评论和答案后,我开始知道从 DataGridView 没有直接方法 到 Insert/Update Sqlite 数据库。所以我很好奇,如果有像 RowSelected
这样的事件会
- 选择一行时触发并获取该行的数据
- 然后将该行的数据放入多个文本框中,最后
- 触发 Insert/Update 从这些文本框中获取值的查询
通过一个按钮
我知道没有示例代码这是高度假设,但这是因为我要的是事件名称。
我认为您不会发现 DataGridView 和 DataTable 自动将内容持久保存到后端 SQLite 数据库的神奇方法。正如您所暗示的那样,我认为您将不得不依赖事件。
我想你错过的活动在这里得到了解答Whosebug cell value changed event
好的,我认为使用 CellEndEdit 更好,因为只有当您单击其他行时才会触发 LeaveRow。
看看这个例子。
你必须
使用:da.Update(ds.Tables("SubjectsList").DefaultView)
Imports System.Data.SqlClient
Public Class Form1
Const connectionstring As String = "server=(local);database=TestDB;uid=sa;pwd=sa;"
Private SQL As String = "select * from customer"
Private con As New SqlConnection(connectionstring)
Private dt As New DataTable
Private adapter As New SqlDataAdapter(SQL, con)
Private commandbuilder As New SqlCommandBuilder(adapter)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
adapter.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
adapter.Update(dt)
End Sub
结束Class
在 LeaveRow 事件或 CellEndEdit 上调用此方法
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
Dim i as integer
Try
for i = 0 to datagrid.rows.count-1
myUpdate(datagrid.item(0,i).tostring() , datagrid.item(1,i).tostring())
next
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
请注意,您还可以使用网格的列名代替列索引,例如 datagrid.item("fname",i).tostring()
这里我们将保存在数据库中:
Sub myUpdate(byval fname as string , byval lname as string)
Try
dim con as new sqlconnection("you connection string")
dim cmd as new sqlcommand
con.open()
cmd.connection= con
cmd.commandtext="insert into table (fname,lname) values (@fname,@lname)"
cmd.Parameters.AddWithValue("@fname", fname)
cmd.Parameters.AddWithValue("@lname", lname)
cmd.ExecuteNonQuery()
Con.close()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End sub
希望能帮到您解决!
有很多方法可以操作数据。
克里斯蒂
请使用此代码,希望它能正常工作
Imports System.Data.SQLite
Public Class Form
Dim con As New SQLite.SQLiteConnection
Dim da As New SQLite.SQLiteDataAdapter
Dim dt As New DataTable
Dim cmdbl As New SQLite.SQLiteCommandBuilder
Dim dlgResult As DialogResult
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
dlgResult = MessageBox.Show("Do you want to save the changes you made?", "Confirmation!", MessageBoxButtons.YesNo)
If dlgResult = DialogResult.Yes Then
Try
con.Open()
cmdbl = New SQLiteCommandBuilder(da)
da.Update(dt)
MsgBox("Updated successfully!")
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
End Class
我正在将来自 SQLite table 的数据显示到 DataGridView 中,如下所示 -
Private Sub Subjects_Manager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New SQLiteConnection("Data Source = c:\demo\test.db;Version=3;")
con.Open()
sql = "SELECT * FROM med_subjects"
da = New SQLiteDataAdapter(sql, con)
da.Fill(ds, "SubjectsList")
DataGridView1.DataSource = ds.Tables("SubjectsList").DefaultView
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Subject Id"
.Columns(1).HeaderCell.Value = "Subject Name"
End With
DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
con.close()
End Sub
我想将在 DataGridView 中完成的更改(Row/s 的更新或 Row/s 的插入)保存回 SQLite table。但是我找不到办法。
编辑 1 : 我知道 SQLite 的 Insert/Update 查询,但我不知道如何以及在何处保存它们,以便可以触发它们以响应 DataGridView 中所做的更改。例如
' I am using this variable for demonstration, in reality InsertSubjectSqlString will be equal to changes done in DataGridView
Dim InsertSubjectSqlString As String = "Insert into med_subjects (Subject_Name) Values ('_Miscellaneous')"
Dim SqliteInsertRow As SQLiteCommand
SqliteInsertRow = con.CreateCommand
SqliteInsertRow.CommandText = InsertSubjectSqlString
SqliteInsertRow.ExecuteNonQuery()
但是我不知道,我应该把它放在哪里?
编辑 2:
看到评论和答案后,我开始知道从 DataGridView 没有直接方法 到 Insert/Update Sqlite 数据库。所以我很好奇,如果有像 RowSelected
这样的事件会
- 选择一行时触发并获取该行的数据
- 然后将该行的数据放入多个文本框中,最后
- 触发 Insert/Update 从这些文本框中获取值的查询 通过一个按钮
我知道没有示例代码这是高度假设,但这是因为我要的是事件名称。
我认为您不会发现 DataGridView 和 DataTable 自动将内容持久保存到后端 SQLite 数据库的神奇方法。正如您所暗示的那样,我认为您将不得不依赖事件。
我想你错过的活动在这里得到了解答Whosebug cell value changed event
好的,我认为使用 CellEndEdit 更好,因为只有当您单击其他行时才会触发 LeaveRow。
看看这个例子。
你必须
使用:da.Update(ds.Tables("SubjectsList").DefaultView)
Imports System.Data.SqlClient
Public Class Form1
Const connectionstring As String = "server=(local);database=TestDB;uid=sa;pwd=sa;"
Private SQL As String = "select * from customer"
Private con As New SqlConnection(connectionstring)
Private dt As New DataTable
Private adapter As New SqlDataAdapter(SQL, con)
Private commandbuilder As New SqlCommandBuilder(adapter)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
adapter.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
adapter.Update(dt)
End Sub
结束Class
在 LeaveRow 事件或 CellEndEdit 上调用此方法
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
Dim i as integer
Try
for i = 0 to datagrid.rows.count-1
myUpdate(datagrid.item(0,i).tostring() , datagrid.item(1,i).tostring())
next
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
请注意,您还可以使用网格的列名代替列索引,例如 datagrid.item("fname",i).tostring()
这里我们将保存在数据库中:
Sub myUpdate(byval fname as string , byval lname as string)
Try
dim con as new sqlconnection("you connection string")
dim cmd as new sqlcommand
con.open()
cmd.connection= con
cmd.commandtext="insert into table (fname,lname) values (@fname,@lname)"
cmd.Parameters.AddWithValue("@fname", fname)
cmd.Parameters.AddWithValue("@lname", lname)
cmd.ExecuteNonQuery()
Con.close()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End sub
希望能帮到您解决!
有很多方法可以操作数据。
克里斯蒂
请使用此代码,希望它能正常工作
Imports System.Data.SQLite
Public Class Form
Dim con As New SQLite.SQLiteConnection
Dim da As New SQLite.SQLiteDataAdapter
Dim dt As New DataTable
Dim cmdbl As New SQLite.SQLiteCommandBuilder
Dim dlgResult As DialogResult
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
dlgResult = MessageBox.Show("Do you want to save the changes you made?", "Confirmation!", MessageBoxButtons.YesNo)
If dlgResult = DialogResult.Yes Then
Try
con.Open()
cmdbl = New SQLiteCommandBuilder(da)
da.Update(dt)
MsgBox("Updated successfully!")
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
End Class