By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Guid questions

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Dean Slindee" <sl*****@charter.net> wrote in message
news:fp*******************@fe04.lga...
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

Nov 21 '05 #2

P: n/a
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.
"Dean Slindee" <sl*****@charter.net> wrote in message
news:fp*******************@fe04.lga...
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

Nov 21 '05 #3

P: n/a
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 uniqueidentifier type.

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

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

"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

Nov 21 '05 #4

P: n/a
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.