如何从Visual Basic 2013 .net中的DataGridView更新SQLite数据库?

时间:2015-01-16 09:28:30

标签: vb.net visual-studio sqlite datagridview visual-studio-2013

我将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中所做的更改时触发它们。 e.g。

' 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这样的事件

  • 触发选择行并获取该行的数据
  • 然后将行的数据放入多个文本框中,最后
  • 触发从这些文本框中获取值的插入/更新查询 按钮

我知道这是非常假设的没有样本代码,但是因为我要求提供事件名称。

3 个答案:

答案 0 :(得分:1)

在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

我希望这会帮助你解决! 有很多方法可以操纵数据 CristiC

答案 1 :(得分:0)

我认为你不会发现DataGridView和DataTable将自动保存到后端SQLite数据库的一些神奇方法。正如你暗示我认为你将不得不依赖事件。

我认为你遗失的事件在这里得到解答stackoverflow cell value changed event

答案 2 :(得分:0)

好吧我认为最好使用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

结束班