以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  下面,重复地写代码 ,怎么优化好呢?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=122102)

--  作者:yancheng
--  发布时间:2018/7/18 22:58:00
--  下面,重复地写代码 ,怎么优化好呢?
Sele ct Case e.DataCol.Name
    Case "数量"
        Dim dr As DataRow = DataTables("发票库存").find(" 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\'  ")
            Dim drs As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'收进发票\' ")
            Dim Values As new List(of  String )
            For Each dr2 As DataRow In drs
                Values.Add( CStr(dr2("autoid")))
            Next
            Dim str As String = String.Join("\',\'",Values.ToArray)
            Dim drs1 As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'开出发票\' ")
            Dim Values1 As  new List(of  String)
            For Each dr1 As DataRow In drs1
                Values1.Add( CStr(dr1("autoid")))
            Next
            Dim str1 As String = String.Join("\',\'",Values1.ToArray)
        If dr IsNot Nothing Then
            dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
            dr("出库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str1 & "\')  ")
            dr.save
         Else
             Dim n As Integer = DataTables("发票库存").SQLCompute(" max(autoid) ") +1
             Dim dd As Row = Tables("发票库存").AddNew()
             dd("AutoID") = n
             dd("名称") = e.DataRow("名称")
             dd("规格") = e.DataRow("规格")
             dd("单位") = e.DataRow("单位")
             dd("单价") = e.DataRow("单价")
             dd("含税单价") = e.DataRow("含税单价")
             dd.save
            dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
            dr("出库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str1 & "\')  ")
            dr.save
        End If


上面的代码 中重复的是:
            dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
            dr("出库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str1 & "\')  ")

怎么才能可以不重复,来优化代码 呢?

--  作者:有点甜
--  发布时间:2018/7/18 23:15:00
--  
Select Case e.DataCol.Name
    Case "数量"
        Dim dr As DataRow = DataTables("发票库存").find(" 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\'  ")
        Dim drs As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'收进发票\' ")
        Dim Values As new List(of  String )
        For Each dr2 As DataRow In drs
            Values.Add( CStr(dr2("autoid")))
        Next
        Dim str As String = String.Join("\',\'",Values.ToArray)
        Dim drs1 As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'开出发票\' ")
        Dim Values1 As  new List(of  String)
        For Each dr1 As DataRow In drs1
            Values1.Add( CStr(dr1("autoid")))
        Next
        Dim str1 As String = String.Join("\',\'",Values1.ToArray)
        If dr Is Nothing Then
            Dim n As Integer = DataTables("发票库存").SQLCompute(" max(autoid) ") +1
            Dim dd As Row = Tables("发票库存").AddNew()
            dd("AutoID") = n
            dd("名称") = e.DataRow("名称")
            dd("规格") = e.DataRow("规格")
            dd("单位") = e.DataRow("单位")
            dd("单价") = e.DataRow("单价")
            dd("含税单价") = e.DataRow("含税单价")
            dd.save
           
        End If
        dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
        dr("出库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str1 & "\')  ")
        dr.save
End Select

--  作者:yancheng
--  发布时间:2018/7/18 23:40:00
--  
好像不行,
如果没有dr行,执行:
 dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
之前,会弹错;因没有找到DR;

--  作者:有点甜
--  发布时间:2018/7/18 23:46:00
--  
elect Case e.DataCol.Name
    Case "数量"
        Dim dr As DataRow = DataTables("发票库存").find(" 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\'  ")
        Dim drs As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'收进发票\' ")
        Dim Values As new List(of  String )
        For Each dr2 As DataRow In drs
            Values.Add( CStr(dr2("autoid")))
        Next
        Dim str As String = String.Join("\',\'",Values.ToArray)
        Dim drs1 As List(Of DataRow) = DataTables("发票主表").Select(" 业务类型 = \'开出发票\' ")
        Dim Values1 As  new List(of  String)
        For Each dr1 As DataRow In drs1
            Values1.Add( CStr(dr1("autoid")))
        Next
        Dim str1 As String = String.Join("\',\'",Values1.ToArray)
        If dr Is Nothing Then
            Dim n As Integer = DataTables("发票库存").SQLCompute(" max(autoid) ") +1
            dr = dataTables("发票库存").AddNew()
            dr("AutoID") = n
            dr("名称") = e.DataRow("名称")
            dr("规格") = e.DataRow("规格")
            dr("单位") = e.DataRow("单位")
            dr("单价") = e.DataRow("单价")
            dr("含税单价") = e.DataRow("含税单价")
            
        End If
        dr("入库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str & "\') ")
        dr("出库数量") = DataTables("发票内容明细表").SQLCompute("sum(数量)"," 名称 =  \'" & e.DataRow("名称") & "\' and 规格 =  \'" & e.DataRow("规格") & "\' and 单位 =  \'" & e.DataRow("单位") & "\' and MainID in (\' " &  str1 & "\')  ")
        dr.save
End Select