以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  求高手给看下有什么问题?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=43298)

--  作者:jnletao
--  发布时间:2013/12/4 8:56:00
--  求高手给看下有什么问题?
以下代码用于网络多用户下的唯一编号,不要求可以连续或补号,但一定要保证唯一不重复。但实际应用中还是发现有多次重复。找不出原因!
请高手指正到底哪里出了错?

CutRecord_BeforeAddDataRow

 

For Each dr As DataRow In e.DataTable.DataRows

    If dr.RowState = DataRowState.Added Then

        MessageBox.Show("请先保存或撤销当前行,才能新增!","【裁剪记录单】提示", MessageBoxButtons.OK ,MessageBoxIcon.Information)

        e.Cancel = True

    End If

Next



CutRecord_DataRowAdded

 

Dim d As Date = Date.Today

Dim y As Integer = d.Year

Dim m As Integer = d.Month

Dim Days As Integer = Date.DaysInMonth(y,m)

Dim fd As Date = New Date(y,m,1) \'获得该月的第一天

Dim ld As Date = New Date(y,m,Days) \'获得该月的最后一天

Dim bh As String ="CUT" & Format(d,"yyMM") \'生成编号的前6位,4位年,2位月.

If e.DataRow("CutRecordID").StartsWith(bh) = False \'如果编号的前6位不符

    Dim max As String

    Dim idx As Integer

    max = e.DataTable.Compute("Max(CutRecordID)","CutDate >= #" & fd & "# And CutDate <= #" & ld & "#") \'取得该月的最大编号

    If max > "" Then \'如果存在最大编号

        idx = CInt(max.Substring(8,3)) + 1 \'获得最大编号的后三位顺序号,并加1

    Else

        idx = 1 \'否则顺序号等于1

    End If

    e.DataRow("CutRecordID") = bh & "-" & Format(idx,"000")

End If




CutRecord_BeforeSaveDataRow

 

Dim dr As DataRow = e.DataRow

If dr.RowState <> DataRowState.Added Then \'如果不是新增行

    Return \'那么返回

Else

    \'==========

    Dim CCutRecordID As String

    Dim cmd As New SQLCommand

    cmd.C

    cmd.CommandText = "Select [CutRecordID] From {CutRecord} Where [CutRecordID] = \'" & e.DataRow("CutRecordID") & "\'"

    CCutRecordID = cmd.ExecuteScalar()

    \'==========

    If CCutRecordID IsNot Nothing Then

        Dim d As Date = Date.Today

        Dim y As Integer = d.Year

        Dim m As Integer = d.Month

        Dim Days As Integer = Date.DaysInMonth(y,m)

        Dim fd As Date = New Date(y,m,1) \'获得该月的第一天

        Dim ld As Date = New Date(y,m,Days) \'获得该月的最后一天

        Dim bh As String ="CUT" & Format(d,"yyMM") \'生成编号的前6位,4位年,2位月.

       

        Dim max As String

        Dim idx As Integer

        cmd.CommandText = "Select Max([CutRecordID]) From {CutRecord} Where CutDate >= \'" & fd & "\' And CutDate <= \'" & ld & "\'"

        max = cmd.ExecuteScalar() \'取得该月的最大编号

        \' MessageBox.Show("max" & max)

        If max > "" Then \'如果存在最大编号

            idx = CInt(max.Substring(8,3)) + 1 \'获得最大编号的后三位顺序号,并加1

        Else

            idx = 1 \'否则顺序号等于1

        End If

        e.DataRow("CutRecordID") = bh & "-" & Format(idx,"000")

    End If

    \'==========

End If


--  作者:Bin
--  发布时间:2013/12/4 9:04:00
--  
http://www.foxtable.com/help/topics/1994.htm
--  作者:jnletao
--  发布时间:2013/12/4 9:36:00
--  
帮助上看了,只是想请看下我这段代码有什么问题?
--  作者:Bin
--  发布时间:2013/12/4 9:38:00
--  
单纯看代码,没看出问题,没有例子测试分析不好说.

你直接按照帮助的做肯定不会出问题.

--  作者:有点甜
--  发布时间:2013/12/4 20:38:00
--  
 用max重复的可能性虽小,但肯定会有重复的。

 你试试用_Identify + 前缀来做编号吧。肯定是唯一的。

--  作者:狐狸爸爸
--  发布时间:2013/12/5 8:37:00
--  

多用户不能这样编码的,应该:

http://www.foxtable.com/help/topics/1994.htm