Check if table is part of DBC

topic: 

It is easy to find out if a table belongs to the open Database Container (DBC) using INDBC() function. Sometimes it's necessary to check it w/o referring to DBC itself.

The low level file functions (LLFF) can be used to open a table as a file and check its header for backlink to the DBC. The Table Header Record Structure is documented in VFP help under Table File Structure

.


Note 1 Presence of the backlink does not guarantee that table is a part of DBC.
This is sample code. Add error handling and adjust to your requirements as necessary.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

? IsTableInDBC("C:\myfolder\mytable.dbf")
RETURN

FUNCTION IsTableInDBC
LPARAMETERS tcTableName
LOCAL lnOffset, lcBuffer, lcRetVal
* Open table low-level as a text file
lnFh1 = FOPEN(tcTableName)
IF lnFh1 < 0
	lcRetVal = "***CANNOT OPEN TABLE***"
	RETURN lcRetVal
ENDIF
* Make sure that's VFP table
lcBuffer = FREAD(lnFh1, 1)
DO CASE
CASE INLIST(ASC(lcBuffer), 0x03, 0x83, 0xF5)
	* FoxPro (pre VFP) table
	lcRetVal = "***FREE TABLE***"
CASE NOT INLIST(ASC(lcBuffer), 0x30, 0x31, 0x32)
	= FCLOSE(lnFh1)
	* not VFP table
	lcRetVal = "***NOT VFP TABLE***"
	RETURN lcRetVal
OTHERWISE
	* Continue	
ENDCASE
* Position of the first data record - 2 bytes
= FSEEK(lnFh1, 8, 0)
* Offset to the beginning of the DBC name - 263 bytes back
lnOffset = ASC(FREAD(lnFh1,1)) + ASC(FREAD(lnFh1,1)) * 256 - 263
= FSEEK(lnFh1, lnOffset, 0)
* The DBC name or 0x00's if it's free table
lcBuffer = FREAD(lnFh1, 263)
= FCLOSE(lnFh1)
IF ASC(lcBuffer) = 0
	lcRetVal = "***FREE TABLE***"
ELSE
	* table in dbc, return DBC name
	lcRetVal = LEFT( lcBuffer, AT( CHR(0), lcBuffer) - 1)
ENDIF

RETURN lcRetVal


Comments

You can also use :
1
2
CURSORGETPROP("Database", ALIAS())