Asp.net +函数内的Call函数

时间:2017-12-14 09:01:22

标签: asp.net sql-server function

我在asp.net vb类文件中有一个函数,它生成一个唯一的字母数字字符串。

生成字符串后,我想检查字符串是否已存在于sql表中,如果是,我想重复该过程。

请找到我正在使用的代码

    Public Function GenerateUniqueID() As String    
     Dim numbers As String = "1234567890"
     Dim characters As String = numbers
     Dim length As Integer = 5
     Dim UID As String = String.Empty
     For i As Integer = 0 To length - 1
        Dim character As String = String.Empty
        Do
            Dim index As Integer = New Random().Next(0, characters.Length)
            character = characters.ToCharArray()(index).ToString()
        Loop While otp.IndexOf(character) <> -1
        UID += character
     Next

  '-----------
  check if UID already exist in db
  if yes

     repeat same function to generate new ID

  if no

     return unique id

   '-----------
     Return UID
End Function

我想知道如果数据库中已存在生成的ID,我该如何调用相同的函数。

1 个答案:

答案 0 :(得分:0)

不要在前端生成随机字符串,而是在SQL Server本身中尝试。请尝试以下

DECLARE @UID NVARCHAR(255),@Row INT =0

WHILE ISNULL(@Row,0) =0
BEGIN
    SELECT @UID = NEWID()
    SELECT @Row = COUNT(1) FROM YourTable WHERE AlphaString = @UID

END

SELECT AlphaString = @UID