在MySql数据库中插入DataGrid值

时间:2014-11-28 12:59:38

标签: mysql vb.net datagridview

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

enter image description here

要删除当前选择的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 ?)"

1 个答案:

答案 0 :(得分:0)

这只是快而又脏。实际上你应该做这个参数化的

你只需要选择它,所以当前不是什么

    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