我的代码没有将数据值存储到Sql数据库中

时间:2017-01-26 04:20:44

标签: mysql vb.net

我写了一个代码来将文本框的值存储到sql数据库中并且它工作正常但是当我尝试将datagridview的值插入到sql数据库中时,它既不工作也不返回错误.....

Private Sub savenprint()
        If Not db.con.State = ConnectionState.Open Then
            db.dbOpen()
            With db.cmd
                .Connection = db.con
                .CommandText = "INSERT INTO Reciept VALUES ('" & txtRecieptNo.Text & "','" & TransDate.Text & "','" & txtSubTotal.Text & "') "
                .ExecuteNonQuery()
            End With
            'to save data of DGVOrderedList into RecieptDescription
            For Each row As DataGridViewRow In DGVorderedList.Rows

                MsgBox(row.Cells(1).Value)
                With db.cmd
                    .Connection = db.con
                    .CommandText = "INSERT INTO billDescription VALUES ('" & Val(txtRecieptNo.Text) & "','" & row.Cells(1).Value & "','" & row.Cells(2).Value & "','" & row.Cells(4).Value & "','" & row.Cells(3).Value & "','" & row.Cells(5).Value & "')"
                End With
                MsgBox("Record Saved!")
            Next



        End If
        db.dbClose()
    End Sub

1 个答案:

答案 0 :(得分:1)

            With db.cmd
                .Connection = db.con
                .CommandText = "INSERT INTO billDescription VALUES ('" & Val(txtRecieptNo.Text) & "','" & row.Cells(1).Value & "','" & row.Cells(2).Value & "','" & row.Cells(4).Value & "','" & row.Cells(3).Value & "','" & row.Cells(5).Value & "')"
                .ExecuteNonQuery()
            End With