日期:2014-05-20  浏览次数:20896 次

高分求! ADO.NET之DataView 如何高效过滤!!!
有如下表:
表1:Dealer
field:
Dealer_ID char(10) PK
...

数据量:2行

表2:Dealer_category
field:
Dc_ID char(10) PK
dealer_ID char(10)
...

数据量:274行

表3:Dealer_product
field:
Dp_ID char(10) PK
Product_name nvarchar(50)
...

数据量:5462行

表4:Product_category
field:
Pc_ID char(10) PK
Dp_ID char(10)
Dc_ID char(10)
...

数据量:9109行

表5:Product_ready
field:
Pr_ID char(10) PK
Dp_ID char(10)
...

数据量:467行

在WinForm里操作,上述5表均完全读入DataTable缓存

Dp_list为DataGridview
需要在Dp_list内显示Dealer_product内容

Treeview1用树显示Dealer_category,点击treenode时,用dpsource对Dp_list进行过滤(条件:当前treenode及childnode中包含的Dc_ID所对应的Dp_ID,以及不在Product_ready中的DpID)

ado.net新人,目前只会简单操作,用下列代码,4秒以上才过滤完成。

求高效的方法!拜谢!

VB.NET code

 Private dpsource As New DataView(mydataset.Tables("Dealer_product")) 'DPlist数据源
    Private dcstr As String '当前选择的dcIDs

    '过滤dplist
    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        getnodevalue()
        dplist_show(getdp(dcstr))
    End Sub

  '取当前选择node的值
    Private Sub getnodevalue()
        dcstr = Chr(39) & currnode.Name.ToString & Chr(39)
        If currnode.GetNodeCount(False) > 0 Then
            For Each tmpnode As TreeNode In currnode.Nodes
                dcstr &= " , '" & tmpnode.Name.ToString & Chr(39)
            Next
        End If
        'MsgBox("currnode value:" & dcstr)
    End Sub

    '取得当前dc的DPs
    Private Function getdp(Optional ByVal _Dc_ID As String = Nothing) As String
        If _Dc_ID = Nothing Then
            Return " "
        Else
            Dim dpstr As New StringBuilder
            For Each tmpdp As DataRow In mydataset.Tables("Product_category").Select("Dc_ID in (" & _Dc_ID & ")")
                Dim findrow As DataRow() = mydataset.Tables("Product_ready").Select("Dp_ID = '" & tmpdp("Dp_ID").ToString & "'")
                If findrow.Length = 0 Then
                    dpstr.Append("'" & tmpdp("Dp_ID").ToString & "',")
                End If
            Next
            If dpstr.Length > 0 Then
                dpstr.Remove(dpstr.Length - 1, 1)
                Return dpstr.ToString
            Else
                Return Nothing
            End If
        End If

    End Function

    '过滤Dplist
    Private Sub dplist_show(Optional ByVal _Dp_ID As String = Nothing)
        If _Dp_ID = Nothing Then
            dpsource.RowFilter = "Dp_ID = '1 '"
        Else
            dpsource.RowFilter = "Dp_ID in (" & _Dp_ID & ") and use_status = '使用中'"

        End If
    End Sub



------解决方案--------------------
你的数据快要上1W了,建议你直接根据条件查询数据库返回,而不要怎么多数据都存到DATATABLE中。
------解决方案--------------------
确实速度不快,不过我也用RowFilter,帮顶!
------解决方案--------------------
帮顶......
------解决方案--------------------
帮你UP
------解决方案--------------------
UPPPP
------解决方案--------------------
Mr. YHVH 22:32:44
 ADO.NET,多少年没人用的名字了。
Mr. YHVH 22:37:10
4秒?!
Mr. YHVH 22:37:13
人才。。。。。
Mr. YHVH 22:39:24
 干吗不用LINQ。
George 22:40:15
他不是写了select么。那不是linq么
Mr. YHVH 22:40:27
 无语。。。。。。
Mr. YHVH 22:41:01
 不过就算这样也不至于要4秒如此夸张,定然是到处瞎整。。。。
George 22:41:03
之前一直用2005,刚开始看linq,谁知道2010都出来了……
Mr. YHVH 22:42:45
 
Mr. YHVH 22:42:59
代码写太烂,鉴定完毕。。。。。
Mr. YHVH 22:43:10
直接用LINQ,应该在一秒内。
George 22:43:15
介家伙,消失的真不是时候,难得iv帮点评……
George 22:43:39
用linq吧2个foreach合并么?
George 22:48:07
vb10中居然还保留了模块的概念……
Mr. YHVH 22:49:01
 IN本来就是错误的。
Mr. YHVH 22:49:08
应该用JOIN。
Mr. YHVH 22:49:21
这不是数据库,不会自动帮你优化的。
Mr. YHVH 22:50:27
 最好是直接做一个GroupJoin,然后缓存这个GroupJoin结果。
Mr. YHVH 22:51:32
理论上直接在DataSet建立关系(DataRelation),然后建立DataView,效率蹭蹭的就上来了。
Mr. YHVH 22:51:32
理论上直接在DataSet建立关系(DataRelation),然后建立DataView,效率蹭蹭的就上来了。

------------------------------------

谁看懂了。写句linq……
------解决方案--------------------
你怎么可以发带颜色的标题??????
------解决方案--------------------
UP..UP............