C# guid and SQL uniqueidentifier

前端 未结 5 1787
你的背包
你的背包 2020-11-27 15:37

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

5条回答
  •  孤街浪徒
    2020-11-27 16:22

    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.

提交回复
热议问题