C# guid and SQL uniqueidentifier

前端 未结 5 1748
你的背包
你的背包 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.

    0 讨论(0)
  • 2020-11-27 16:23
    // 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();
    
    0 讨论(0)
  • 2020-11-27 16:26

    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();
            }
        }
    
    0 讨论(0)
  • 2020-11-27 16:42

    You can pass a C# Guid value directly to a SQL Stored Procedure by specifying SqlDbType.UniqueIdentifier.

    Your method may look like this (provided that your only parameter is the 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

    0 讨论(0)
  • 2020-11-27 16:42

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

    0 讨论(0)
提交回复
热议问题