Skip to content

Instantly share code, notes, and snippets.

@Sheeo
Created July 25, 2011 19:14
Show Gist options
  • Save Sheeo/1104920 to your computer and use it in GitHub Desktop.
Save Sheeo/1104920 to your computer and use it in GitHub Desktop.
Not the way to add rows to a table
Public Shared Function GenerateNewUniqueID() As String
Dim returnString As String = Guid.NewGuid.ToString()
Dim unique As Boolean = False
Using conn As New SqlConnection(My.Settings.onlinePDFConnectionString)
conn.Open()
Dim commString As String = "SELECT Count(*) FROM Books WHERE UniqueID = '{0}%'"
Dim comm As New SqlCommand(String.Format(commString, returnString.Substring(0, 13)), conn)
While Not unique
comm.CommandText = String.Format(commString, returnString.Substring(0, 13))
Dim count As Integer = comm.ExecuteScalar()
If count > 0 Then
returnString = Guid.NewGuid.ToString()
Else
unique = True
End If
End While
End Using
Return returnString
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment