【问题标题】:Insert in MySql database DataGrid Values在 MySql 数据库中插入 DataGrid 值
【发布时间】:2023-03-22 04:46:01
【问题描述】:

我想将我在 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 ?)"

【问题讨论】:

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

标签: mysql vb.net datagridview


【解决方案1】:

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

你只需要选择它,这样 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    相关资源
    最近更新 更多