tags:

views:

3087

answers:

5

I want to create a GUID and store it in the DB.

In C# a guid can be created using Guid.NewGuid(). This creates a 128 bit integer. SQL Server has a uniqueidentifier column which holds a huge hexidecimal number.

Is there a good/preferred way to make C# and SQL Server guids play well together? (i.e. create a guid using Guid.New() and then store it in the database using nvarchar or some other field ... or create some hexidecimal number of the form that SQL Server is expecting by some other means)

+5  A: 

There is a SQL type for this: uniqueidentifier.

mjv
Yep, its in the question :) I'm asking how you make uniqueidentifier play well with C# Guid.NewGuid()
Daniel
Didn't you say in your question that you're using nvarchar as the field's data type?
Jay Riggs
You will use the uniqueidentifier type in SQL Server, then do as Peter demontrates above to store values to it.
Tor Haugen
Perfect, thanks Tor
Daniel
Have to agree with Pierre-Alain Vigeant - the method in Peter's answer is *not* the best way to do this.
MusiGenesis
+5  A: 

SQL is expecting the GUID as a string. The following in C# returns a string Sql is expecting.

"'" + Guid.NewGuid().ToString() + "'"

Something like

INSERT INTO TABLE (GuidID) VALUE ('4b5e95a7-745a-462f-ae53-709a8583700a')

is what it should look like in SQL.

Peter Oehlert
If you use SqlParameter, you don't have to convert the GUID into a string.
Pierre-Alain Vigeant
To be clearer to everyone, I'm not suggesting that the sql specified should be used as demonstrated from C# with string concatenation. The crux of the question, I believe, is what form does a SQL UNIQUEIDENTIFIER LITERAL look like. The literal looks just like a string but with a special format. The two code snippets demonstrate what the literal looks like in 2 languages. I agree with DLKG that using a parameterized query and passing the guid as a typed parameter is both more performant as well as preventing potential SQL Injection attacks.
Peter Oehlert
@Peter: the question was literally "Is there a good/preferred way to make C# and SQL Server guids play well together?" In your code sample, C# is stealing SQL Server's lunch money. :P
MusiGenesis
+5  A: 

Store it in the database in a field with a data type of uniqueidentifier.

Jay Riggs
A: 

It's far better to use stored procedures to communicate with your SQL database, in which case you'd declare the parameter type as OleDbType.Guid and pass a C# value of type Guid.

If you must construct SQL strings, you can surround it with single quotes as previously mentioned.

Jared
+8  A: 

Here's a code snipit showing how to insert a Guid using a parametrised 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();
        }
    }
DLKJ
guidValue should be @guidValue
Peter Oehlert
Fixed the typo, cheers.
DLKJ
+1 for demonstrating the correct way to do it, rather than just answering the question literally.
Christian Hayter
Very useful. I was trying to parse a string etc... inside the database. This works great. Cheers, ~ck
Hcabnettek