提问人:Scorpion_RJ 提问时间:9/10/2023 最后编辑:Dan GuzmanScorpion_RJ 更新时间:9/10/2023 访问量:39
将 DataGridView contect 重新保存到数据库中
Resave the DataGridView contect into database
问:
我正在将DataGridView数据保存到运行正常的SQL数据库中。我想重新保存网格,但首先想从数据库中删除相同编号 PO 的现有行。为此,我在插入数据库之前使用了相同 PO 编号的 Delete 命令。
现在,如果插入过程中出现错误,它会删除现有行并显示错误。
我希望如果出现错误,则无法运行删除过程。如何检查?
Private Sub SaveData()
Try
DeleteData()
Dim i As Integer
Dim answer As Integer
answer = MsgBox("Are you sure to save?", vbQuestion + vbYesNo + vbDefaultButton1, "Save Record")
If answer = vbYes Then
ConnecDatabase()
Sql = "Operation_InsertPO"
For i = 0 To DGV_PO.RowCount - 1
cmd = New SqlCommand
With cmd
.Connection = Conn
.Parameters.Clear()
.CommandType = CommandType.StoredProcedure
.CommandText = Sql
.Parameters.AddWithValue("@PONo", text_ponumber.Text)
.Parameters.AddWithValue("@ProductCode", DGV_PO.Rows(i).Cells(2).Value)
.Parameters.AddWithValue("@ProductName", DGV_PO.Rows(i).Cells(3).Value)
.Parameters.AddWithValue("@Units", DGV_PO.Rows(i).Cells(4).Value)
.Parameters.AddWithValue("@Qty", DGV_PO.Rows(i).Cells(5).Value)
.Parameters.AddWithValue("@Rate", DGV_PO.Rows(i).Cells(6).Value)
.Parameters.AddWithValue("@Amt", DGV_PO.Rows(i).Cells(7).Value)
.Parameters.AddWithValue("@User", GlobalVariables.UserName)
.ExecuteNonQuery()
End With
Next
cmd.Dispose()
Conn.Close()
MsgBox("Saved Successfuly", vbInformation)
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
Finally
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
End Try
End Sub
Private Sub DeleteData()
Try
ConnecDatabase()
Sql = "Operation_DeletePO"
cmd = New SqlCommand
With cmd
.Connection = Conn
.Parameters.Clear()
.CommandType = CommandType.StoredProcedure
.CommandText = Sql
.Parameters.AddWithValue("@pono", text_ponumber.Text)
.ExecuteNonQuery()
End With
Conn.Close()
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
Finally
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
End Try
End Sub
答: 暂无答案
评论