简体   繁体   English

C# guid 和 SQL uniqueidentifier

[英]C# guid and SQL uniqueidentifier

I want to create a GUID and store it in the DB.我想创建一个 GUID 并将其存储在数据库中。

In C# a guid can be created using Guid.NewGuid().在 C# 中,可以使用 Guid.NewGuid() 创建 guid。 This creates a 128 bit integer.这将创建一个 128 位整数。 SQL Server has a uniqueidentifier column which holds a huge hexidecimal number. SQL Server 有一个 uniqueidentifier 列,其中包含一个巨大的十六进制数。

Is there a good/preferred way to make C# and SQL Server guids play well together?有没有一种好的/首选的方法可以让 C# 和 SQL Server guid 很好地协同工作? (ie 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) (即使用 Guid.New() 创建一个 guid,然后使用 nvarchar 或其他一些字段将其存储在数据库中......或者通过其他方式创建 SQL Server 期望的形式的一些十六进制数)

Here's a code snippet showing how to insert a GUID using a parameterised query:下面的代码片段展示了如何使用参数化查询插入 GUID:

    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();
        }
    }

SQL is expecting the GUID as a string. SQL 期望 GUID 作为字符串。 The following in C# returns a string Sql is expecting. C# 中的以下内容返回 Sql 期望的字符串。

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

Something like就像是

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

is what it should look like in SQL.是它在 SQL 中的样子。

You can pass a C# Guid value directly to a SQL Stored Procedure by specifying SqlDbType .UniqueIdentifier .您可以通过指定SqlDbType .UniqueIdentifier将 C# Guid 值直接传递给 SQL 存储过程。

Your method may look like this (provided that your only parameter is the Guid):您的方法可能如下所示(前提是您的唯一参数是 Guid):

public static void StoreGuid(Guid guid)
{
    using (var cnx = new SqlConnection("YourDataBaseConnectionString"))
    using (var cmd = new SqlCommand {
        Connection = cnx,
        CommandType = CommandType.StoredProcedure,
        CommandText = "StoreGuid",
        Parameters = {
            new SqlParameter {
                ParameterName = "@guid",
                SqlDbType = SqlDbType.UniqueIdentifier, // right here
                Value = guid
            }
        }
    })
    {
        cnx.Open();
        cmd.ExecuteNonQuery();
    }
}

See also: SQL Server's uniqueidentifier另请参阅:SQL Server 的uniqueidentifier

将其存储在数据库中的字段中,其数据类型为 uniqueidentifier。

// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(GentEFONRFFConnection);
myConnection.Open();
SqlCommand myCommand = new SqlCommand("your Procedure Name", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@orgid", SqlDbType.UniqueIdentifier).Value = orgid;
myCommand.Parameters.Add("@statid", SqlDbType.UniqueIdentifier).Value = statid;
myCommand.Parameters.Add("@read", SqlDbType.Bit).Value = read;
myCommand.Parameters.Add("@write", SqlDbType.Bit).Value = write;
// Mark the Command as a SPROC

myCommand.ExecuteNonQuery();

myCommand.Dispose();
myConnection.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM