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

时间:2016-09-04 10:01:25

标签: c# sql-server vb.net datatable

我已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数据

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

如何刷新DataTable以便我可以获取之前已更新的数据?我试图再次重新加载DataTable,但一无所获。

谢谢

1 个答案:

答案 0 :(得分:0)

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

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

当数据库发生变化时,它将重新加载数据表。