Connecting to Microsoft SQL server using FWH simplified ADO functions.
FWH provides a single line function FW_OpenAdoConnection( <parame,,,> ) to connect to any server, MSSQL, ORACLE, MYSQL,MSACCESS, etc.
In case of MSSQL, the server can be configured to login using Windows Authentication (also called Integrated Security) and/or using Username and password. Later is preferable.
The following sample (ado1.prg) demonstrates connecting to MSSQL server using this function and then view all the databases available on the server and also to create a new database "FWHADO" for further tests.
ado1.prg:
- Code: Select all Expand view RUN
#include "fivewin.ch"
#include "adodef.ch"
static oCn
//----------------------------------------------------------------------------//
function Main()
local cServer := "SQLEXPRESS" // your server address
local cInitialDB := "" // for now we keep it empty
local cUser := "SA"
local cPassword := "password" // your password
local aCredentials, aDB
// If connecting by Windows authentication (Integrated Security)
aCredentials := { "MSSQL", cServer, cInitialDB }
// If connecting by Username and Password
aCredentials := { "MSSQL", cServer, cInitialDB, cUser, cPassword }
? "Trying to connect"
oCn := FW_OpenAdoConnection( aCredentials, .T. )
if oCn == nil
? "Failed to connect"
return nil
endif
? "Successfully connected to server"
// Let us view list of databases existing on the server
aDB := FW_AdoCatalogs( oCn )
XBROWSER aDB TITLE "List of databases"
if AScan( aDB, { |c| Upper( c ) == "FWHADO" } ) == 0
// Database FWHADO does not exist
// We crete it now
oCn:Execute( "CREATE DATABASE FWHADO" )
? "Created Database FWHADO"
endif
oCn:Close()
return nil
//----------------------------------------------------------------------------//
If the above sample successfully works, then we can go to the second example (ado2.prg).
This sample connects to the server with initial database FWHADO, imports one DBF from the \fwh\samples folder and opens the newly imported table for edit. We can edit, append new records as we like.
ado2.prg
- Code: Select all Expand view RUN
#include "fivewin.ch"
#include "adodef.ch"
REQUEST DBFCDX
static oCn
//----------------------------------------------------------------------------//
function Main()
local cServer := "SQLEXPRESS" // your server address
local cInitialDB := "FWHADO"
local cUser := "SA"
local cPassword := "password" // your password
local aCredentials, oRs
// If connecting by Windows authentication (Integrated Security)
aCredentials := { "MSSQL", cServer, cInitialDB }
// If connecting by Username and Password
aCredentials := { "MSSQL", cServer, cInitialDB, cUser, cPassword }
? "Trying to connect"
oCn := FW_OpenAdoConnection( aCredentials, .T. )
if oCn == nil
? "Failed to connect"
return nil
endif
? "Successfully connected to server"
? "Importing DBF"
FW_AdoImportFromDBF( oCn, "c:\fwh\samples\states.dbf" ) // give your dbf with full path
// Open table
oRs := FW_OpenRecordSet( oCn, "SELECT * FROM STATES" )
if oRs == nil
? "Failed to open table"
else
XBROWSER oRs FASTEDIT AUTOSORT
oRs:Close()
endif
oCn:Close()
return nil
//----------------------------------------------------------------------------//
We can view all the ADO functions in \fwh\source\function\adofuncs.prg
To learn all about ADO
https://www.w3schools.com/asp/ado_intro.asp