我已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
,但一无所获。
谢谢
答案 0 :(得分:0)
解决! 我只是在启动索引循环时重新加载datareader的代码并刷新数据表。
dt.reset()
dtreader = cmddt.ExecuteReader()
Dim dt As New System.Data.DataTable()
dt.Load(dtreader)
当数据库发生变化时,它将重新加载数据表。