【问题标题】:C# guid and SQL uniqueidentifierC# guid 和 SQL 唯一标识符
【发布时间】:2010-11-28 23:38:59
【问题描述】:

我想创建一个 GUID 并将其存储在数据库中。

在 C# 中,可以使用 Guid.NewGuid() 创建 guid。这将创建一个 128 位整数。 SQL Server 有一个 uniqueidentifier 列,其中包含一个巨大的十六进制数。

有没有一种好的/首选的方法可以让 C# 和 SQL Server guid 很好地协同工作? (即使用 Guid.New() 创建一个 guid,然后使用 nvarchar 或其他字段将其存储在数据库中......或通过其他方式创建 SQL Server 期望的表单的一些十六进制数字)

【问题讨论】:

  • 不要使用 nvarchar,使用 uniqueidentifier。

标签: c# sql guid uniqueidentifier


【解决方案1】:

SQL 需要将 GUID 作为字符串。 C# 中的以下内容返回 Sql 所期望的字符串。

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

有点像

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

是它在 SQL 中的样子。

【讨论】:

  • 如果使用SqlParameter,则不必将GUID转换为字符串。
  • 为了让大家更清楚,我并不是建议指定的 sql 应该像 C# 中演示的那样使用字符串连接。我相信,问题的症结在于 SQL UNIQUEIDENTIFIER LITERAL 是什么样的形式。文字看起来就像一个字符串,但具有特殊的格式。两个代码 sn-ps 演示了文字在 2 种语言中的样子。我同意 DLKG 的观点,即使用参数化查询并将 guid 作为类型化参数传递既能提高性能,又能防止潜在的 SQL 注入攻击。
  • 在实体框架,数据库优先方法中,uniqueidentifier SQL 数据类型(@TorHaugen 在评论中推荐)转换为System.Guid C# 数据类型,这是 OP 询问的内容
  • 那么 System.Guid 如何生成数据库中尚未存在的标识符?虽然,可能性很小......是否有唯一性检查?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这里的代码 sn-p 展示了如何使用参数化查询插入 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();
            }
        }
    

    【讨论】:

    • guidValue 应该是@guidValue
    • +1 用于演示正确的方法,而不是仅仅从字面上回答问题。
    • 非常有用。我试图在数据库中解析字符串等。这很好用。干杯,~ck
    • 这适用于插入但不适用于WHERE 子句。它尝试将 c# 中的小写 guid 与大写 uniqueidentifier 进行比较,结果不匹配。
    【解决方案4】:

    您可以通过指定 @987654321@.UniqueIdentifier 将 C# Guid 值直接传递给 SQL 存储过程。

    您的方法可能如下所示(前提是您的唯一参数是 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();
        }
    }

    参见:SQL Server 的uniqueidentifier

    【讨论】:

      【解决方案5】:
      // 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多