Guid questions

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

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
 
Dean Slindee said:
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


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() = RowThatContainsGuidId.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
 
SQL 2000 has a "uniqueidentifier" 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
"uniqueidentifier" field in your tables is generally a good idea.
 
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.Tables("Items").NewRow
r.Item("KeyID") = New Guid(get_NewItemID())
DBDataSet.Tables("Items").Rows.Add(r)
DBCmd = New OleDb.OleDbCommand("SELECT * FROM Items", DBConn)
DBAdapt.SelectCommand = 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.DayOfYear, 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() = ConvIntegertoByteArray(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top