Switch to GUIDs - changing the way I code?

  • Thread starter Thread starter Masahiro Ito
  • Start date Start date
M

Masahiro Ito

When using Integer as PK, I would often have functions that would return
the Integer when searching for a specific row in a datatable.

I am trying to switch to GUID. Before, if the function returned a zero, I
would assume the PK didn't exist. Now, if I make a call to lookup the
GUID, and that row doesn't exist yet, I can't seem to figure out the
simplest way - without doing a redundant function to check if exists first
- causing two loops.

Simple Example of my Integer based Code:
Table1 - TableID(Int), Description(Nvarchar)

Private Function GetTableIDByDescription(sDescription as String) as Integer
Private iID as integer = 0
For each row
If Description = sDescription Then iID = Row.TableID
Return iID

Private i as integer = GetTableIDByDescription(id)
If i = 0 then
' New Record - insert
Else
' Use the ID


With GUID?:
Table1 - TableID(GUID), Description(Nvarchar)

Private Function GetTableIDByDescription(sDescription as String) as GUID
Private iID as New Guid ' ??
For each row
If Description = sDescription Then iID = Row.TableID
Return iID

Now, without two functions (one for Exists, one for GetID), is there a way
that I can rewrite all these code sections?

Masa
 
Masahiro,

use the Select method on the datatable

dim drs as datarow() = dt.select("Description")
if drs.length>0 then
return ""
else
return drs(0)("GUID")
end if

-Sam Matzen

Another way of saying it is to assign the GUID in your program rather than
letting the database assigne it.
 
Samuel L Matzen said:
use the Select method on the datatable
dim drs as datarow() = dt.select("Description")
if drs.length>0 then
return ""
else
return drs(0)("GUID")
end if

Thanks Samuel. That is a much easier way!

Masa
 
Back
Top