Home Articles

如何使用SqlCommand语句刷新更新后的数据表?

Asked
Viewed 746 times
1

我有 DataTable 加载了 SqlDataReader 查询 . 加载 DataTable 后,我循环使用 SqlCommand 每个索引插入数据 . 但 DataTable 没有刷新 . 如何在数据库中更新数据时刷新 DataTable

connectionx()
        Dim dtreader As SqlDataReader
        Dim cmddt As New SqlCommand("SELECT * from sometable", con)

        dtreader = cmddt.ExecuteReader()

        Dim dt As New System.Data.DataTable()
        dt.Load(dtreader) 'Here from datareader


        For index As Integer = currentRow To dt.Rows.Count - 1
            Dim drR As DataRow = dt.NewRow()
            Dim reader1 As SqlDataReader

            Dim EP As DateTime
            Dim EndW As String = dtnow.ToString("MM/dd/yyyy HH:mm")
            Dim setEndWaiting As Integer = Convert.ToInt32(adapterSch.SetEndWTime(EndW, Convert.ToInt32(Session("ShipLoadingOrderFK"))))


                Dim startPre As DateTime = EndW
                Dim endPre As DateTime = startPre + TimeSpan.Parse(defPre)
                Dim setPreDocumentTime As Integer = Convert.ToInt32(adapterSch.SetPreDocumentTime(startPre, endPre, Convert.ToInt32(Session("ShipLoadingOrderFK"))))

                Dim startPump As DateTime = endPre
                Dim endPump As DateTime
                Dim shipKey As String = Shipid.SelectedValue
                pumpigResult(MinutePumpResult, HoursPumpResult, FlowrateValue, Shipid.SelectedValue, Loading_No.Value, typeLoading.SelectedValue)
                If FlowrateValue = 0 Or FlowrateValue = "" Then
                    endPump = endPre + TimeSpan.Parse(defPumping)
                Else
                    endPump = endPre + New TimeSpan(HoursPumpResult, MinutePumpResult, 0)
                End If
                Dim setPumpTime As Integer = Convert.ToInt32(adapterSch.SetPumpingTime(startPump, endPump, Convert.ToInt32(Session("ShipLoadingOrderFK"))))

                Dim startPost As DateTime = endPump
                Dim endPost As DateTime = endPump + TimeSpan.Parse(defPost)
                Dim setPostTime As Integer = Convert.ToInt32(adapterSch.SetPostTime(startPost, endPost, Convert.ToInt32(Session("ShipLoadingOrderFK"))))

插入数据后,每个索引上面都有代码 . 我已获得前一行(index-1)的值,但我得到 OLD Data before updated .

Dim EP as datetime = Convert.ToDateTime(dt.Rows(index - 1)("PostDocEnd").ToString())

如何刷新 DataTable 以便我可以获取已更新的先前数据?我已经尝试重新加载 DataTable ,但什么都没有 .

谢谢

1 Answer

  • 0

    解决了!我只是在启动索引循环时重新加载datareader的代码并刷新数据表 .

    dt.reset()
        dtreader = cmddt.ExecuteReader()
        Dim dt As New System.Data.DataTable()
        dt.Load(dtreader)
    

    它将在数据库更改时重新加载数据表 .

Related