[英]How to update an Access DB from a DataGridView in Visual Basic
[英]How to update SQLite database from DataGridView in Visual Basic 2013 .net?
我正在将数据从SQLite表显示到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中完成的更改(行/秒的更新或行/秒的插入)保存回SQLite表 。 但是我找不到办法。
编辑1:我知道SQLite的插入/更新查询,但我不知道如何以及在哪里保留它们,以便可以在对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插入/更新Sqlite数据库。 所以我很好奇,是否有像RowSelected
这样的事件
我知道没有任何示例代码是高度假设,但这是因为我要求提供事件名称。
在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
注意,您也可以使用网格的列名代替列index,例如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
希望这可以帮助您解决!
有许多方法可以处理数据。
CristiC
我认为您不会发现DataGridView和DataTable会自动将事物持久化到后端SQLite数据库的一种神奇方法。 正如您所暗示的,我认为您将不得不依靠事件。
我认为您所缺少的事件已在此处回答stackoverflow单元值更改事件
好的,我认为最好使用CellEndEdit,因为仅当您单击另一行时才会触发LeaveRow。
看这个例子。
你必须
使用: da.Update(ds.Tables(“ SubjectsList”)。DefaultView)
Imports System.Data.SqlClient
公开课表格1
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
最终班
请使用此代码,希望它能正常工作
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.