Sequential GUID in Linq-to-Sql?

C# (safe) code (Compliments of the NHibernate Guid Comb Generator) Guid GenerateComb() { byte[] destinationArray = Guid.NewGuid().ToByteArray(); DateTime time = new DateTime(0x76c, 1, 1); DateTime now = DateTime.Now; TimeSpan span = new TimeSpan(now.Ticks – time.Ticks); TimeSpan timeOfDay = now.TimeOfDay; byte[] bytes = BitConverter.GetBytes(span.Days); byte[] array = BitConverter.GetBytes((long) (timeOfDay.TotalMilliseconds / 3.333333)); Array.Reverse(bytes); Array.Reverse(array); Array.Copy(bytes, bytes.Length – … Read more

Why does Guid.ToByteArray() order the bytes the way it does?

If you read the Examples section from the GUID constructor, you’ll find your answer: Guid(1,2,3,new byte[]{0,1,2,3,4,5,6,7}) creates a Guid that corresponds to “00000001-0002-0003-0001-020304050607”. a is a 32-bit integer, b is a 16-bit integer, c is a 16-bit integer, and d is simply 8 bytes. Because a, b, and c are integer types rather than raw … Read more

C# guid and SQL uniqueidentifier

Here’s a code snippet showing how to insert a GUID using a parameterised query: using(SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using(SqlTransaction trans = conn.BeginTransaction()) using (SqlCommand cmd = conn.CreateCommand()) { cmd.Transaction = trans; cmd.CommandText = @”INSERT INTO [MYTABLE] ([GuidValue]) VALUE @guidValue;”; cmd.Parameters.AddWithValue(“@guidValue”, Guid.NewGuid()); cmd.ExecuteNonQuery(); trans.Commit(); } }

What is a good unique PC identifier?

Some good identifiers: MAC Address: It’s fairly easy to get at, and it’s usually unique. However, it can be spoofed/changed rather easily, so it depends on how unique it needs to be. CPU Serial Number: It’s not available on lots of older systems, but it’s there. Check out this MSDN page. It won’t change, but … Read more

Generating GUIDs in Ruby

As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid function. For example: require ‘securerandom’ SecureRandom.uuid # => “96b0a57c-d9ae-453f-b56f-3b154eb10cda”

Sequential Guid Generator

You could just use the same Win32 API function that SQL Server uses: UuidCreateSequential and apply some bit-shifting to put the values into big-endian order. And since you want it in C#: private class NativeMethods { [DllImport(“rpcrt4.dll”, SetLastError=true)] public static extern int UuidCreateSequential(out Guid guid); } public static Guid NewSequentialID() { //Code is released into … Read more

How Random is System.Guid.NewGuid()? (Take two)

The answer is: You should not need to know this. As stated in the accepted answer to a related question: A GUID doesn’t make guarantees about randomness, it makes guarantees around uniqueness. An even stronger statement on security and randomness is made in RFC4122, which speficies the UUID format: Do not assume that UUIDs are … Read more