如何使用SqlCommand语句更新值后刷新数据表? [英] How to refresh the datatable after updated value with SqlCommand statement?

查看:54
本文介绍了如何使用SqlCommand语句更新值后刷新数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 DataTable ,该表已加载 SqlDataReader 查询.加载 DataTable 后,我循环执行每个索引都使用 SqlCommand 插入数据的过程.但是 DataTable 不会刷新.在数据库中更新数据时,如何刷新 DataTable ?

I have DataTable that was loaded with a SqlDataReader query. After my DataTable is loaded, I do looping that I was insert data with a SqlCommand every index. But the DataTable is not refreshed. How to refresh the DataTable while data is updated in the database?

    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数据.

After insert data while every index with code above. I have get the value on previous row (index-1), but I get the OLD Data before updated.

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

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

How to refresh the DataTable so that I can get the previous data that has been updated? I have try to re-load DataTable again, but is nothing.

谢谢

推荐答案

已解决!我只是在开始索引循环时就为重载datareader设置了行代码,并刷新了数据表.

Solved! I just line code for reload datareader at starting index looping and refresh the datatable.

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

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

It will be reload the datatable while database is changed.

这篇关于如何使用SqlCommand语句更新值后刷新数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆