Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating PK for MTS objects

Status
Not open for further replies.

tchbansi

Programmer
Aug 17, 2000
2
CA
Im building an application with MTS and SQLServer.
Im facing the problem of generating unique ID (primary key) to my objects.
I was thinking of creating a class for generating the PK. but I dont see how to do it in VB+MTS.
anyone has any ideas?
 
Why do you generate unique ID (primary key) to objects.
Any example???
 
Example: Order and OrderDetails.
You have 2 classes: cOrder and cOrderDetail that are persist in the table t_Order and t_OrderDetail(id,, with t_OrderDetail having a foreign key pointing to t_Order.

When you want to save a new order+its details, you need to generate a unique id for cOrder to be able to save the cOrderDetails objects (for the foreign key).
 
In Delphi,it is easy to handle:

in middle tier:
.build master-detail relation between tblOrder and tblOrderDetail
.package the data to client through TDataSetProvider

in client tier:
.recreate the master-detail relation by TClientDataSet

 

'Here is a Class I use for Generating Guids
'Just Reference It and ask for GenerateGUID
Option Explicit
'Class Generates GUIDS
'DFW 07/25/00
Private Type Guid
Data1 As Long
Data2 As Long
Data3 As Long
Data4(8) As Byte
End Type
Private Declare Function CoCreateGuid _
Lib "ole32.dll" (pguid As Guid) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" _
(rguid As Any, ByVal lpstrClsId As Long, _
ByVal cbMax As Long) As Long
'Gets the Computer name
Private Declare Function GetComputerName& Lib "kernel32" Alias "GetComputerNameA" (ByVal Buffer As String, nSize As Long)

Public Function GenerateGUID() As String
'***************************************************
'Purpose: Function to Generate GUID
'Author: David F. Wade
'Date: 07/25/00

Dim uGUID As Guid
Dim sGUID As String
Dim bGUID() As Byte
Dim lLen As Long
Dim RetVal As Long
lLen = 40
bGUID = String(lLen, 0)
CoCreateGuid uGUID
'Convert the structure into a displayable string
RetVal = StringFromGUID2(uGUID, VarPtr(bGUID(0)), lLen)
sGUID = bGUID
If (Asc(Mid$(sGUID, RetVal, 1)) = 0) Then RetVal = RetVal - 1
GenerateGUID = Left$(sGUID, RetVal)
End Function
Public Function GetGuid() As String
Dim strGUID As String
strGUID = GenerateGUID
strGUID = Mid(strGUID, 2, 36)
GetGuid = Replace(strGUID, "-", "")

End Function

 
Have you considered using a hierachical recordset. It takes care of your problem more efficiently. You can get the recordset from the server as a connectionless recordset; update the records on the fly and when done you can send the recordset to the server and commit changes using updatebatch.

This works fine for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top