如何将数据表从datagridview保存到mysql并使用列和行标题文本将其重新加载到datagriview索引

时间:2018-12-08 11:26:10

标签: vb.net datagridview

我有问题,如何将数据表从datagridview保存到mysql,然后将其重新加载到数据库中到datagridview

这是datagridview数据表

------------------------------------------------ 
          | Item A     | Item B     |  Item C  |   
----------------------------------------------
Value     |      1     |      2     |     4    |  
----------------------------------------------
Location  |      4     |      3     |     2    |  
----------------------------------------------
Price     |      3     |      3     |     3    |  
----------------------------------------------

这是我的代码

Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
    Dim cmd As MySqlCommand


    For j As Integer = 0 To rowcount - 1
        For i As Integer = 0 To columncount - 1
            cmd = New MySqlCommand("insert into `tb_score` values('" & DataGridView1.Columns(i).HeaderText & "','" & DataGridView1.Rows(j).HeaderCell.Value & "','" & DataGridView1.Item(i, j).Value & "')", Connector)
            cmd.ExecuteNonQuery()
        Next
    Next

    MsgBox("Data saved")
End Sub



Sub LoadingDatabase()
    Dim rs As MySqlDataReader
    Dim cmd As MySqlCommand

    For j As Integer = 0 To rowcount - 1
        For i As Integer = 0 To columcount- 1
            cmd = New MySqlCommand("select score  from `tb_score` where `Item`='" & DataGridView1.Columns(i).HeaderText & "' and `Criteria`='" & DataGridView1.Rows(j).HeaderCell.Value & "'", Connector)

            rs = cmd.ExecuteReader
            If rs.HasRows Then
                While rs.Read
                    DataGridView1.Item(i, j).Value = rs("score")
                End While
                rs.Close()
            Else
                rs.Close()
            End If
        Next
    Next
End Sub

0 个答案:

没有答案