将datagridview中的所有行插入数据库中的一行

问题描述 投票:0回答:1

我正在处理我的项目,我想将我的 datagridview 中的所有记录保存在一个 id 中,但结果,我的数据库中只插入了一行

table picture 如您所见,有两行,但是当我单击提交时,只有一行会保存到我的数据库中。

database picture

这是我在提交按钮中的代码

        Dim p_id As Integer
        Dim p_description As String = Nothing
        Dim p_from As String = Nothing
        Dim p_to As String = Nothing
        Dim table As New DataTable
        For i As Integer = 0 To DataGridView2.Rows.Count = 1

            p_id = DataGridView2.Rows(i).Cells(0).Value
            p_description = DataGridView2.Rows(i).Cells(1).Value
            p_from = DataGridView2.Rows(i).Cells(2).Value
            p_to = DataGridView2.Rows(i).Cells(3).Value


        Next
'sql insert query'

sql_query = sql_query = "INSERT INTO Petition_tbl(p_id,p_name_of_petitioner,p_item_no,p_description,p_from,p_to)VALUES('"petition_id.Text.Trim"''" & petitioner_name.Text.Trim & "','" & p_id & "','" & p_description & "','" & p_from & "','" & p_to & "')"

任何人都可以帮助我,将不胜感激。

sql-server vb.net
1个回答
0
投票

这里是将数据行放入未绑定到数据源的 DataGridView (dgvTX) 的代码,在本例中是 SQLite DB

                Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
                'dgvTX.DataSource = rdr
                'Statement Above is when DB is bound to dgvTX
                '============================================

                While rdr.Read()
                    intID = CInt((rdr("TID")))
                    strDate = rdr("txSortDate").ToString
                    strTxType = rdr("txType").ToString
                    strAmt = CDec(rdr("txAmount"))
                    strCKNum = rdr("txCKNum").ToString
                    strDesc = rdr("txDesc").ToString
                    strBal = CDec(rdr("txBalance"))
                    dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                    dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                    'dgvTX.Columns(6).DefaultCellStyle.Format = "C"'Adds the $ sign and commas
                    dgvTX.Rows.Add(intID, strDate, strTxType, strAmt, strCKNum, strDesc, strBal, emptyStr)

                    rowCount = rowCount + 1

                End While
© www.soinside.com 2019 - 2024. All rights reserved.