Sie können auch meinen Kennungsgenerator verwenden. Aber es ist leider kein Integer. Stellen Sie sicher, dass Sie die Groß-/Kleinschreibung in ID-Spalte in db ändern, andernfalls ändern Sie den Zeichenbereich. Der Bezeichner ist URL freundlich. Basiert auf partiellen DateTime Ticks (10 Zeichen) und partiellen Random (6 Zeichen). Es ist nicht sortierbar. Verwenden Sie stattdessen die AddedDate-Spalte, um eine Zeilensequenz zu erhalten. Verwenden Sie varchar(16)
Spalte Typ und SQL_Latin1_General_CP1_CS_AS
Kollation.
public static class IdentifyGenerator
{
private static char[] sybmols = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
};
public static string WebHash()
{
int length = sybmols.Length;
ulong num = (ulong)DateTime.Now.Ticks;
string output = string.Empty;
ulong tmp = num;
ulong mod = 0;
while (tmp != 0)
{
mod = tmp % (ulong)length;
tmp = tmp/(ulong)length;
output = sybmols[mod] + output;
}
output += RandomString(6);
return output;
}
public static string RandomString(int length)
{
Stack<byte> bytes = new Stack<byte>();
string output = string.Empty;
for (int i = 0; i < length; i++)
{
if (bytes.Count == 0)
{
bytes = new Stack<byte>(Guid.NewGuid().ToByteArray());
}
byte pop = bytes.Pop();
output += sybmols[(int)pop % sybmols.Length];
}
return output;
}
}
Einheit Test:
[TestClass]
public class Code
{
[TestMethod]
public void IdentifyGeneratorTest()
{
var set = new HashSet<string>();
for (int i = 1; i <= 1000000; i++)
{
var id = IdentifyGenerator.WebHash();
if (!set.Add(id))
Assert.Fail("IdentifyGenerator duplicate found");
}
}
}
Viel Glück.
Können Sie erklären, warum Sie in der DB dagegen sind? – Oded