local o
o = createobject("PopListFromSQLServer")
o.show(1)
define class PopListFromSQLServer as form
height = 193
width = 337
docreate = .t.
autocenter = .t.
caption = "Populate Listbox from SQL Server"
name = "frmPopListFromSQLServer"
dimension aEmployees[1] && Rowsource for the listbox
add object lstemployees as listbox with ;
rowsourcetype = 5, ;
rowsource = "thisform.aEmployees", ;
height = 168, ;
left = 24, ;
top = 24, ;
width = 192, ;
integralheight = .t., ;
name = "lstEmployees"
add object label1 as label with ;
autosize = .t., ;
backstyle = 0, ;
caption = "Employees", ;
height = 17, ;
left = 24, ;
top = 5, ;
width = 64, ;
name = "lblEmployees"
procedure load
this.GetEmployeeNames()
endproc
protected procedure GetEmployeeNames
* It's much better to create a connection pool to SQL Server
* at some higher level and then to request a connection from
* the pool. Building and tearing down connections on the fly
* is verrrrry slow....
* There are so many ways of accomplishing this,
* it'd be ridiculous to try and cover them here :-)
#define adOpenForwardOnly 0
#define adLockReadOnly 1
#define adCmdText 1
local oConnection, cConnectionString, oEmployees, cSelectStatement
oConnection = createobject("adodb.connection")
* Using integrated security
cConnectionString = ;
"driver={SQL Server};server=[red]YOUR_SERVER_NAME_HERE[/red];database=Pubs"
oConnection.ConnectionTimeout = 10 && Bump up for internet or slow intranet connections
oConnection.open(cConnectionString)
* Check connection state here <-
oEmployees = createobject("ADODB.Recordset")
cSelectStatement = "select fname, minit, lname from employee order by lname, fname"
oEmployees.CursorType = adOpenForwardOnly
oEmployees.LockType = adLockReadOnly
oEmployees.open(cSelectStatement, oConnection, , , adCmdText)
* Check query execution state here <-
local i, cFName, cMName, cLName
i = 0
do while !oEmployees.eof
i = i + 1
dime this.aEmployees[i]
cFName = allt(oEmployees.fields[0].value)
cMName = allt(oEmployees.fields[1].value)
cLName = allt(oEmployees.fields[2].value)
this.aEmployees[i] = ;
cFName + " " + ;
iif(!empty(cMName), cMName + ". ", "") + ;
cLName
oEmployees.MoveNext()
enddo
oEmployees.close
oConnection.close
oEmployees = null
oConnection = null
endproc
enddefine