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
"Dean Slindee" wrote:
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