Foxtable(狐表)用户栏目专家坐堂 → 求高效简洁的代码,高手请指点


  共有9931人关注过本帖树形打印复制链接

主题:求高效简洁的代码,高手请指点

帅哥哟,离线,有人找我吗?
唐尸三摆手
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:928 积分:7769 威望:0 精华:13 注册:2008/9/1 11:58:00
求高效简洁的代码,高手请指点  发帖心情 Post By:2011/3/15 8:37:00 [只看该作者]

在工厂的生产日历中会涉及到避开周日的因素,但是由于erp软件不够智能,所以只好做一次“体外治疗”,下面的代码就是针对周日做的处理,但是效率出其的慢
Dim st As Date = Date.Now
Dim cmd As New SQLCommand
Dim dt As DataTable
Dim ct AS Integer
Dim ct1 AS Integer
Dim ct2 AS Integer
Dim ct3 AS Integer
Dim ct4 AS Integer
Dim ct5 AS Integer
Dim ct6 AS Integer

cmd.C
cmd.CommandText="select * from aec00000 where centercode='0100' and requiredate between '2011-02-27' and '2011-03-10'"
dt=cmd.ExecuteReader()
For Each dr As DataRow In dt.datarows
    Dim d1 As Date=dr("requiredate")
    Dim d2 As Date
    Dim d3 As Date
    Dim wk As Byte=d1.DayOfWeek
    If wk=2 Then   '如果总装日期是周二,那么
        d2=dr("requiredate").AddDays(-3)
        d3=dr("requiredate").AddDays(-5)
    Else If wk=5 Then    '如果总装日期是周五,那么
        d2=dr("requiredate").AddDays(-2)
        d3=dr("requiredate").AddDays(-4)
    Else  '如果总装日期是周一?周三?周四或周六?周日,那么
        d2=dr("requiredate").AddDays(-2)
        d3=dr("requiredate").AddDays(-5)
    End If
'-总装线日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d1 &"',outputdate='"& d1 &"' from aeg00000 where GTcode='"& dr("planid") &"' And childlevel=0 And deptcode='0148'"
    ct1=cmd.ExecuteNonQuery()
    '-装配一线日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d1 &"',outputdate='"& d1 &"' from aeg00000 where GTcode='"& dr("planid") &"' And childlevel=1 And deptcode='0102'"
    ct2=cmd.ExecuteNonQuery()
    '-装配组件日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d2 &"',outputdate='"& d2 &"' from aeg00000 where GTcode='"& dr("planid") &"' And workcentercode='010212'"
    ct3=cmd.ExecuteNonQuery()
    '-装配二线日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d2 &"',outputdate='"& d2 &"' from aeg00000 where GTcode='"& dr("planid") &"' And childlevel=1 And deptcode='0143'"
    ct4=cmd.ExecuteNonQuery()
    '-成型一车间日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d2 &"',outputdate='"& d2 &"' from aeg00000 where GTcode='"& dr("planid") &"' And deptcode='0112'"
    ct5=cmd.ExecuteNonQuery()
    '-成型二车间日期设定
    cmd.CommandText="update aeg00000 set inputdate='"& d3 &"',outputdate='"& d2 &"' from aeg00000 where GTcode='"& dr("planid") &"' And deptcode='0113'"
    ct6=cmd.ExecuteNonQuery()
Next
ct=ct1+ct2+ct3+ct4+ct5+ct6 
Messagebox.Show( "本次操作总共影响" & ct & "行!耗时: " & (Date.Now - st).TotalSeconds & "秒")


只要一算就死机,看来是代码带冗余了,请高手指点精简代码。
红色部分大约有2000行数据结果,估计可能是判断周六周日的过程中发生死机的
[此贴子已经被作者于2011-3-15 8:41:32编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
狐狸爸爸
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251048 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2011/3/15 9:00:00 [只看该作者]

如果2000行数据,你还不如直接载入这2000行到FoxTable处理,要高效很多。

你现在是2000*6 = 12000,要执行12000次Update语句,慢是正常的。


 回到顶部