在MySql数据库中插入DataGrid值

在MySql数据库中插入DataGrid值,mysql,vb.net,datagridview,Mysql,Vb.net,Datagridview,我想将我在datagrid视图中输入的值添加到mysql数据库中 要删除当前选定的ID,我使用此代码 Dim reader As MySqlDataReader Try SqlConn.Open() Dim query As String query = "delete from where id='" & DataGridView1.CurrentRow.Index & "'" Dim c

我想将我在datagrid视图中输入的值添加到mysql数据库中

要删除当前选定的ID,我使用此代码

       Dim reader As MySqlDataReader
    Try
        SqlConn.Open()
        Dim query As String
        query = "delete from  where id='" & DataGridView1.CurrentRow.Index & "'"
        Dim command As New MySqlCommand
        command = New MySqlCommand(query, SqlConn)
        reader = command.ExecuteReader
        MessageBox.Show("Data deleted")
        ucitaj()
        SqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        SqlConn.Dispose()
    End Try
但是如何直接从数据网格视图添加。我在空字段中输入值,当我单击“添加”时,我想添加它。我知道要添加mysql数据库,我需要使用这个查询

query = "insert into base (id, Username, password, Link) values(Which ones ?)"

这只是又快又脏。事实上,您应该将其参数化

您只需选择它,使currentrow不为空

    If Not DataGridView1.CurrentRow Is Nothing Then
        Dim id As Integer = DataGridView1.CurrentRow.Cells("ID").Value
        Dim username As String = DataGridView1.CurrentRow.Cells("username").Value
        Dim pw As String = DataGridView1.CurrentRow.Cells("password").Value
        Dim link As String = DataGridView1.CurrentRow.Cells("Link").Value

        Dim sqlstr As String = "insert into base (id, Username, password, Link) values(" & id & "," & username & "," & pw & "," & link & ")"
        'your code
    End If

由于DGV是数据绑定的,因此无法直接添加记录。。您可以随查询添加并刷新DGV..@matzone我想随查询添加,但我在DGV中输入的值无法在DGV中添加新行。。