Guid questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean Slindee

    #1

    Guid questions

    I would like to use a Guid as a record identifier for to prevent update
    collisions (not as the key, but as a substitute for a timestamp field).

    What is the proper way to define storage for a guid in VB?

    What is the proper way to define storage for a guid in SQL Server?



    Thanks,

    Dean Slindee


  • Mythran

    #2
    Re: Guid questions


    "Dean Slindee" <slindee@charte r.net> wrote in message
    news:fp0oe.4650 4$rt1.11543@fe0 4.lga...[color=blue]
    >I would like to use a Guid as a record identifier for to prevent update
    > collisions (not as the key, but as a substitute for a timestamp field).
    >
    > What is the proper way to define storage for a guid in VB?
    >
    > What is the proper way to define storage for a guid in SQL Server?
    >
    >
    >
    > Thanks,
    >
    > Dean Slindee
    >
    >[/color]


    VB.Net:
    Dim guidId As Guid = Guid.NewGuid()

    I believe in SQL Server, you define a GUID column as Timestamp. If that is
    not it, then it most likely is the Image data type...one or the other should
    work for you :)

    Also, if you want SQL Server to create it for you, you can use the Timestamp
    data type and set the Allows Null to false.

    Snip:

    Dim guidId As Byte() = RowThatContains GuidId.GuidId

    guidId would then contain your Guid...I'm not sure if .Net's Guid is the
    same as SQL Server's timestamp column <shrug> something to learn I guess :)

    Mythran

    Comment

    • Robin Tucker

      #3
      Re: Guid questions

      SQL 2000 has a "uniqueidentifi er" as a datatype. This is basically a guid.
      However, you can use the TIMESTAMP type to test for update collisions in
      your table.

      As GUIDS are just random 128 bit numbers, so there is no "time relation"
      between one GUID and another. This makes them very slow as primary keys for
      things like inserts but they are essential for replication, so including a
      "uniqueidentifi er" field in your tables is generally a good idea.


      "Dean Slindee" <slindee@charte r.net> wrote in message
      news:fp0oe.4650 4$rt1.11543@fe0 4.lga...[color=blue]
      >I would like to use a Guid as a record identifier for to prevent update
      > collisions (not as the key, but as a substitute for a timestamp field).
      >
      > What is the proper way to define storage for a guid in VB?
      >
      > What is the proper way to define storage for a guid in SQL Server?
      >
      >
      >
      > Thanks,
      >
      > Dean Slindee
      >
      >[/color]


      Comment

      • Ken Tucker [MVP]

        #4
        RE: Guid questions

        Hi,

        In addition to the other comments. To create a new guid you can
        use guid.newguid.

        http://msdn.microsoft.com/library/de...wguidtopic.asp

        If adding the record from a stored procedure you can use the sql server
        newid function. Field must be of the uniqueidentifie r type.

        http://msdn.microsoft.com/library/de...a-nop_4pt0.asp

        Ken
        ------------------------------

        "Dean Slindee" wrote:
        [color=blue]
        > I would like to use a Guid as a record identifier for to prevent update
        > collisions (not as the key, but as a substitute for a timestamp field).
        >
        > What is the proper way to define storage for a guid in VB?
        >
        > What is the proper way to define storage for a guid in SQL Server?
        >
        >
        >
        > Thanks,
        >
        > Dean Slindee
        >
        >
        >[/color]

        Comment

        • Dennis

          #5
          RE: Guid questions

          I use a GUID in an access database as follows:

          'Create the Table with a column named KeyId of type GUID
          sqlstring = "CREATE TABLE Items(KeyID GUID PRIMARY KEY)

          'To Add a new Row to Table using sql direct into Database;
          Dim rowid As String = get_NewItemID()
          sqlstring = "INSERT INTO Items (KeyID) VALUES ('" & rowid & "')

          'To Add a new Row to Table using a DataSet where DBDataSet is a dataset
          you've created which has the table ITEMS in it.
          Dim r As DataRow
          r = DBDataSet.Table s("Items").NewR ow
          r.Item("KeyID") = New Guid(get_NewIte mID())
          DBDataSet.Table s("Items").Rows .Add(r)
          DBCmd = New OleDb.OleDbComm and("SELECT * FROM Items", DBConn)
          DBAdapt.SelectC ommand = DBCmd
          DBAdapt.Update( DBDataSet, "Items")

          'Function used to create unique GUID using date and time and last ID
          Private Function get_NewItemID() As String
          Static lastid As Long
          Static id1 As Integer
          Dim id2, id3 As Short
          Dim nw As DateTime = DateTime.Now
          Dim thisid As Long = nw.Ticks
          id1 = id1 + 1 : id2 = CType(nw.DayOfY ear, Short) : id3 =
          CType(nw.Year, Short)
          If thisid = lastid Then thisid = thisid + 1
          lastid = thisid
          'You must create a function to covert an integer to a 8 byte array
          Dim b As Byte() = ConvIntegertoBy teArray(thisid, 8)
          Return New Guid(id1, id2, id3, b).ToString
          End Function
          --

          Hope this helps...There's probably a simpler way to do it but this works for
          me.

          Dennis in Houston


          "Dean Slindee" wrote:
          [color=blue]
          > I would like to use a Guid as a record identifier for to prevent update
          > collisions (not as the key, but as a substitute for a timestamp field).
          >
          > What is the proper way to define storage for a guid in VB?
          >
          > What is the proper way to define storage for a guid in SQL Server?
          >
          >
          >
          > Thanks,
          >
          > Dean Slindee
          >
          >
          >[/color]

          Comment

          Working...