以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  网络的复杂编号 如何找相同的编号  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=55727)

--  作者:blackzhu
--  发布时间:2014/8/22 8:51:00
--  网络的复杂编号 如何找相同的编号

Dim dr As DataRow = e.DataRow
Dim
pf As String
If
dr.RowState <> DataRowState.Added Then \'如果不是新增行
    Return
\'那么返回
ElseIf
dr.IsNull("日期") Then \'如果没有输入日期
   
e.Cancel = True \'取消保存此行
   
MessageBox.Show("必须输入日期!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
Else

    pf
= Format(dr("日期"),"yyMM")
\'否则获得编号的前缀,两位年,两位月
End If
Dim
cmd1 As New SQLCommand
Dim
cmd2 As New SQLCommand
Dim
Key As Integer
Dim
nm As String = e.DataTable.name
cmd1
.ConnectionName = "编号" \'设置数据源名称
cmd2
.ConnectionName = "编号"
cmd1
.commandText = "Select Count(*) From [编号] Where [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
If
cmd1.ExecuteScalar = 0 Then \'如果编号表不存在前缀的行,那么增加一行
   
cmd1.commandtext = "Insert Into 编号 (表名, 前缀, 顺序号) Values(\'" & nm & "\',\'" & pf & "\',1)"
   
cmd1.ExecuteNonQuery
End If

cmd1
.commandText = "Select [顺序号] From [编号] Where [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
Do

    Key
= cmd1.ExecuteScalar()
\'从后台获得顺序号
   
cmd2.commandText = "Update [编号] Set [顺序号] = " & (Key + 1) & " Where [顺序号] = " & Key & " And [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
    If
cmd2.ExecuteNonQuery() > 0 Then \'更新顺序号
        Exit Do
\'更新成功则退出循环
    End If
Loop

e
.DataRow("编号") = pf & "-" & Format(Key,"0000")



 帮助的这段代码 如何改可以做到 比如 相同的客户编号下编号是一样的,我这样改貌似不行:


Dim dr As DataRow = e.DataRow
Dim 
pf As String
If 
dr.RowState <> DataRowState.Added Then \'如果不是新增行
    Return 
\'那么返回
ElseIf 
dr.IsNull("日期") Then \'如果没有输入日期
    
e.Cancel = True \'取消保存此行
    
MessageBox.Show("必须输入日期!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
Else

    pf
 = Format(dr("日期"),"yyMM"
\'否则获得编号的前缀,两位年,两位月
End If
Dim 
cmd1 As New SQLCommand
Dim 
cmd2 As New SQLCommand
Dim 
Key As Integer
Dim 
nm As String = e.DataTable.name
cmd1
.ConnectionName = "编号" \'设置数据源名称
cmd2
.ConnectionName = "编号"
cmd1
.commandText = "Select Count(*) From [编号] Where [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
If 
cmd1.ExecuteScalar = 0 Then \'如果编号表不存在前缀的行,那么增加一行
    
cmd1.commandtext = "Insert Into 编号 (表名, 前缀, 顺序号) Values(\'" & nm & "\',\'" & pf & "\',1)"
    
cmd1.ExecuteNonQuery
End If

cmd1
.commandText = "Select [顺序号] From [编号] Where [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
Do

    Key
 = cmd1.ExecuteScalar() 
\'从后台获得顺序号
    
cmd2.commandText = "Update [编号] Set [顺序号] = " & (Key + 1& " Where [顺序号] = " & Key & " And [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
    If 
cmd2.ExecuteNonQuery() > 0 Then \'更新顺序号
        Exit Do 
\'更新成功则退出循环
    End If
Loop

Dim dx As DataRow = e.DataTable.Find("客户编号=\'" & e.DataRow("客户编号") & "\'And 日期= #" & e.DataRow("日期") & "# And [_Identify] <> " & e.DataRow("_Identify"))

If dx Is Nothing Then

    e.DataRow("编号") = pf & Format(Key,"0000")

Else

    e.DataRow("编号") = dx("编号")

End If


 还是产生了不重复的编号


--  作者:Bin
--  发布时间:2014/8/22 8:57:00
--  
Do
    Key
 = cmd1.ExecuteScalar() 
\'从后台获得顺序号
    
cmd2.commandText = "Update [编号] Set [顺序号] = " & (Key + 1& " Where [顺序号] = " & Key & " And [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
    If 
cmd2.ExecuteNonQuery() > 0 Then \'更新顺序号
        Exit Do 
\'更新成功则退出循环
    End If
Loop

应该放到 IF条件里面去
If dx Is Nothing Then
如果还是这样,说明 DX没有找到符合条件的行

--  作者:blackzhu
--  发布时间:2014/8/22 9:08:00
--  
不是很明白思路
--  作者:Bin
--  发布时间:2014/8/22 9:13:00
--  
 找到不到DX再执行 UPdate


找到,就直接取DX编号

--  作者:blackzhu
--  发布时间:2014/8/22 9:21:00
--  


Dim dx As DataRow = e.DataTable.Find("客户编号=\'" & e.DataRow("客户编号") & "\'And 日期= #" & e.DataRow("日期") & "# And [_Identify] <> " & e.DataRow("_Identify"))

If dx Is Nothing Then

Do
    Key
 = cmd1.ExecuteScalar() 
\'从后台获得顺序号
    
cmd2.commandText = "Update [编号] Set [顺序号] = " & (Key + 1& " Where [顺序号] = " & Key & " And [前缀] = \'" & pf & "\' And 表名 = \'" & nm & "\'"
    If 
cmd2.ExecuteNonQuery() > 0 Then \'更新顺序号
        Exit Do 
\'更新成功则退出循环
    End If
Loop

    e.DataRow("编号") = pf & Format(Key,"0000")

Else

    e.DataRow("编号") = dx("编号")

End If


  还是一样 产生了不重复的编号


--  作者:Bin
--  发布时间:2014/8/22 9:24:00
--  
 说明肯定是找不到DX,为什么找不到.你调试分析一下看看.搞不定.上例子.

--  作者:blackzhu
--  发布时间:2014/8/22 9:55:00
--  
BIN 按照你的做法 编号把临时编号当然正式编号保存了



 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:编号.rar



--  作者:Bin
--  发布时间:2014/8/22 9:59:00
--  
Dim dx As DataRow = e.DataTable.Find

改为 Dim dx As DataRow = e.DataTable.SQLFind

--  作者:blackzhu
--  发布时间:2014/8/22 10:13:00
--  
这个我貌似改过 也不行,我再试试!
--  作者:blackzhu
--  发布时间:2014/8/22 10:16:00
--  
BIN  这个还是有问题的,连续新增状态下 输入相同的 客户编号 编号还是会产生不同不重复