vb.net 请教下各位自动生成流水号的问题!
我想得到商品编号 商品编号=分类编号+4位流水号
例如分类编号是0101001 那么商品编号就是01010010001、01010010002……
分类编号是0101002 商品编号就是01010020001、01010020002……
每个不同分类编号后面的流水号都从0001开始生成
求各位帮助!多谢了!
[解决办法]
SQL测试
create table U_ItemTest
(ItemId nvarchar(10))
#Region "生成单号"
Private Function GetItemNum(ByVal ItemNo As String) As String
'0101 001,0000
Dim middle As String = ItemNo
Dim tmp As String = F.G("select Isnull(max(right(ItemId,4)),'0000') from U_ItemTest where left(ItemId,7)='" & middle & "'")
Dim right As String = SumStrAdd(F.Right(tmp, 4), 4, True)
Return middle + right
End Function
#Region "自增方法"
'''<summary>
''' 字符串自增加1
''' </summary>
''' <param name="sStr">待自增的字符串</param>
''' <param name="len">需要自增的字符串的位数,从右算起(最小为 1)</param>
''' <param name="IsAllDecimal">是否全是数字</param>
''' <returns>返回自增后的字符串</returns>
Public Function SumStrAdd(ByVal sStr As String, ByVal len As Integer, Optional ByVal IsAllDecimal As Boolean = False) As String
Dim Add As Boolean = False
Dim s As String = String.Empty
sStr = sStr.ToUpper()
If len = 0 Then
Return Nothing
End If
If sStr.Length > len Then
s = sStr.Substring(0, sStr.Length - len)
sStr = sStr.Substring(sStr.Length - len, len)
End If
If (sStr = String.Empty Or sStr = "") Then
sStr = "1"
sStr = sStr.PadLeft(len, "0")
Return sStr
End If
Dim strArray() As Char
ReDim strArray(len - 1)
sStr = sStr.PadLeft(len, "0")
strArray = sStr.ToCharArray()
If (strArray(len - 1) = "9") Then
If IsAllDecimal Then
strArray(len - 1) = "0"
Add = True
Else
strArray(len - 1) = "A"
Add = False
End If
ElseIf (strArray(len - 1) = "Z") Then
strArray(len - 1) = "0"
Add = True
Else
strArray(len - 1) = Convert.ToChar(Convert.ToInt32(strArray(len - 1)) + 1)
Add = False
End If
If (Add) Then
If (len - 1 > 0) Then
CharAdd(strArray, len - 1, Add, IsAllDecimal)
End If
Return s + New String(strArray)
Else
Return s + New String(strArray)
End If
End Function
Private Sub CharAdd(ByVal strArray() As Char, ByVal len As Integer, ByVal Add As Boolean, ByVal IsAllDecimal As Boolean)
If (strArray(len - 1) = "9") Then
If IsAllDecimal Then
strArray(len - 1) = "0"
Add = True
Else
strArray(len - 1) = "A"
Add = False
End If
ElseIf (strArray(len - 1) = "Z") Then
strArray(len - 1) = "0"
Add = True
Else
strArray(len - 1) = Convert.ToChar(Convert.ToInt32(strArray(len - 1)) + 1)
Add = False
End If
If Add Then
If (len - 1 > 0) Then
CharAdd(strArray, len - 1, Add, IsAllDecimal)
End If
Else
Return
End If
End Sub
#End Region
#End Region