【问题标题】:How to Insert each DataGridView Row into MYSQL DB如何将每个 DataGridView 行插入 MYSQL DB
【发布时间】:2013-09-17 11:55:37
【问题描述】:

我有这个datagridview,它的行是通过单击按钮手动添加的。 我想做的是以某种方式循环遍历每一行并将其插入到 mysql 数据库中

这是我当前的代码:

Public Sub addResBulk()
    Dim cmdAddRes As New MySqlCommand
    Dim addResQry As String
    'field remarks left empty until process complete
    With cmdAddRes
        .Parameters.AddWithValue("@ctrlno", ctrlNo)
        .Parameters.AddWithValue("@resdate", dtp_resDate.Value)
        .Parameters.AddWithValue("@timestart", cbo_start.SelectedValue)
        .Parameters.AddWithValue("@timeend", cbo_end.SelectedValue)
        .Parameters.AddWithValue("@claimdate", claimdate)
        .Parameters.AddWithValue("@borrowerid", tb_borrowerID.Text)
        .Parameters.AddWithValue("@resloc", tb_location.Text)
    End With


    For row As Integer = 0 To dgv_bulk.Rows.Count - 1
        Try
            addResQry = "INSERT INTO tbl_test(ControlNo, bCode, Qty, resDate, timeSTART, timeEND, claimDate, borrowerID, resLocation) VALUES " _
              + "(@ctrlno, @bcode, @qty, @resdate, @timestart, @timeend, @claimdate, @borrowerID, @resloc)"

            If conn.State = ConnectionState.Open Then
                conn.Close()
                conn.Open()
            Else
                conn.Open()
            End If

            'dgv_bulk.Item(1, o).Value

            With cmdAddRes
                .Parameters.AddWithValue("@bcode", dgv_bulk.Item(1, row).Value)
                .Parameters.AddWithValue("@qty", dgv_bulk.Item(2, row).Value)
                qryRes = .ExecuteNonQuery
            End With
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    Next row
End Sub

但是,错误会妨碍将每一行成功插入数据库。 它告诉我参数@ctrlno 已经定义。另一个错误告诉我我需要一个有效且开放的连接...

有什么想法吗?

提前致谢!

【问题讨论】:

    标签: mysql vb.net loops datagridview


    【解决方案1】:

    这也行

    For x As Integer = 0 To yourdatagridviewname.Rows.Count - 1
    
    Dim str1 = "INSERT INTO [expensesTB]([amount],[description],[expensesDate])values(@amount,@description,@expensesDate)"
                Dim com As New OleDb.OleDbCommand(str1, con)
                com.Parameters.AddWithValue("@amount", yourdatagridviewname.Rows(x).Cells(2).Value)
                com.Parameters.AddWithValue("@description", yourdatagridviewname.Rows(x).Cells(1).Value)
                com.Parameters.AddWithValue("@expensesDate", thisyear.ToString("yyyy/MM/dd"))
                com.ExecuteNonQuery()
                com.Dispose()
            Next
    

    【讨论】:

      【解决方案2】:

      如果你想把你在Datagridview中手动输入的数据插入Mysql数据库,试试这个。

      希望这会对你有所帮助。


      Public Sub addResBulk()
      
              ''create connection 
              Dim conn As MySqlConnection = New MySqlConnection(connectionString)
              conn.Open()
      
              Dim comm As MySqlCommand = New MySqlCommand()
              comm.Connection = conn
      
              ''insert data to sql database row by row
              Dim ctrlno, bcode, resdate, timestart, timeend, claimdate, borrowerID, resloc As Object
              Dim qty As Double
              Dim tbl_test As New DataTable
      
      
              For i = 0 To DataGridView1.Rows.Add - 1 Step 1
                  ctrlno = DataGridView1.Rows(i).Cells(0).Value()
                  bcode = DataGridView1.Rows(i).Cells(1).Value()
                  qty = DataGridView1.Rows(i).Cells(2).Value()
                  resdate = DataGridView1.Rows(i).Cells(3).Value()
                  timestart = DataGridView1.Rows(i).Cells(4).Value()
                  timeend= DataGridView1.Rows(i).Cells(5).Value()
                  claimdate = DataGridView1.Rows(i).Cells(6).Value()
                  borrowerID = DataGridView1.Rows(i).Cells(7).Value()
          resloc = DataGridView1.Rows(i).Cells(8).Value()
      
                  comm.CommandText = "insert into tbl_test(ControlNo,bCode,Qty,resDate,timeSTART,timeEND,claimDate,borrowerID,resLocation ) values('" & ctrlno & "','" & bcode & "','" & qty & "','" & resdate & "','" & timestart & "','" & timeend & "','" & claimdate & "','" & borrowerID & "','" & resloc & "')"
                  comm.ExecuteNonQuery()
              Next
              conn.Close()
              Me.Close()
          End If
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2014-08-08
        • 1970-01-01
        • 2017-06-05
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        相关资源
        最近更新 更多