Connection 1: SQL Server Connection

<< Click to Display Table of Contents >>

Navigation:  Examples > Connection >

Connection 1: SQL Server Connection

Previous pageReturn to chapter overviewNext page

This sample show how to make a connection with an Ado DataSource.

The sample uses Ms SQL SERVER provider and shows the usage of :

Prompt Property

Embed user id/password in connectionstring

use user id/password in open method

 

Connection Strings for other providers can be found in the Ado connections topic or the other samples.

 

FUNCTION Start()

 LOCAL sError AS STRING

 LOCAL oConn AS AdoConnection

 LOCAL cbErr AS CODEBLOCK

 LOCAL uError        AS USUAL

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

 BEGIN SEQUENCE

 set color TO w+/b

 cls

 oConn := AdoConnection{}

 oConn:ConnectionTimeout := 5

 oConn:provider := "SQLOLEDB"

 // By setting the prompt property we can determine if a logon dialog is shown:

 oConn:Properties:[item, "Prompt"]:Value := AdPromptComplete

 

 

 // This connection string has embedded user Id/Password and will show no dialog,

 // unless something else is wrong (the database missing for example)

 oConn:ConnectionString := "Data Source=(local);" +;

         "Initial Catalog=pubs;User Id=sa;Password=;"

 

 // The following connection string will show the dialog

 // because it does not have a Username and password

 // If you don't want a dialog, set the Prompt Property to adPromptNever

 

 // oConn:ConnectionString := "Data Source=(local);Initial Catalog=pubs"

 

 

 oConn:CursorLocation := adUseClient

 RECOVER USING uError

 ? "Error: "+uError:description

 quit

 wait

 END

 BEGIN SEQUENCE

 oConn:Open(NIL,NIL,NIL,NIL)

 // Note: you can also use the User Id and Password parameters of

 // the Open Method:

 //oConn:Open(NIL,"sa","",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

 

 ELSE

 ? "Connection succesfull:"

 ? "Provider: "+oConn:Provider

 ? "Database: "+oConn:DefaultDatabase

 ? "String  : "+oConn:ConnectionString

 ENDIF

 wait

 

 

 RETURN