Connection 3: UDL file for Connection string

<< Click to Display Table of Contents >>

Navigation:  Examples > Connection >

Connection 3: UDL file for Connection string

Previous pageReturn to chapter overviewNext page

The sample below shows how to use an UDL file to open a connection. An UDL file is a file external to the program that contains the information needed to open a connection.

UDL files are UNICODE text files, containing the connection string.

 

Note:

if you want to save the password in the UDL file, you have to select the 'Allow saving password' checkbox in the dialog. You can also use the USERID and PASSWORD parameters of the AdoConnection:Open method to specify the username/password from the program

 

 

FUNCTION Start()

 LOCAL sError AS STRING

 LOCAL oConn AS AdoConnection

 LOCAL cbErr AS CODEBLOCK

 LOCAL uError        AS USUAL

 LOCAL sUDL        AS STRING

 sUDL := "C:\VO2Ado26\Sample.UDL"

 cbErr := ErrorBlock({|oErr|_Break(oErr)})

 BEGIN SEQUENCE

 set color TO w+/b

 cls

 IF ! File(sUDL)

 // File is missing, now create one

 // and open it for editing

 FClose(FCreate(sUDL))

 ENDIF

 ? "Now edit the UDL information"

 ? " and press enter when you have saved the connection info"

 ShellExecute(GetActiveWindow(), String2Psz("Open"), ;

 String2Psz(sUDL),NULL_PSZ, NULL_PSZ, 0)

 wait

 oConn := AdoConnection{}

 oConn:ConnectionTimeout := 5

 

 oConn:CursorLocation := adUseClient

 RECOVER USING uError

 ? "Error: "+uError:description

 quit

 wait

 END

 BEGIN SEQUENCE

 oConn:Open("File Name="+sUDL+";",NIL,NIL,NIL)

 RECOVER USING uError

 END

 // Restore error handler

 ErrorBlock(cbErr)

 

 IF oConn:State <> adStateOpen

 // Error occurred

 IF oConn:Errors:Count > 0

 // Ado Error

 sError := oConn:Errors:[Item,1]:Description

 ELSEIF IsInstanceOfUsual(uError, #Error)

 sError := uError:Description

 ELSE

 sError := "Unknown error"

 END IF

 ? "Error when making connection: ", sError

 wait

 ELSE

 ? "Connection succesfull:"

 ? "Provider: "+oConn:Provider

 ? "Database: "+oConn:DefaultDatabase

 ? "String  : "+oConn:ConnectionString

 ? ""

 ? "Now press enter TO see the contents OF the UDL File (UNICODE TEXT !)"

 wait

 WinExec(String2Psz("Notepad "+sUDL), SW_SHOW)

 ENDIF

 RETURN