;FUNCTION DBExecuteSQL()
;
;ACTION Executes a SQL command on a database
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.11 (minor code changes)
; 1.1
;
;DATE CREATED 2002/09/05
;
;DATE MODIFIED 2004/03/07
;
;KIXTART 4.20+
;
;SYNTAX RETCODE = DBEXECUTESQL(CONNECTION, SQL [,CMDTYPE])
;
;PARAMETERS CONNECTION
; open connection object to a data source from DBConnOpen()
;
; SQL
; SQL command to be executed
;
; CMDTYPE
; Optional integer defining the command type (e.g. SQL statement, stored procedure)
;
;RETURN 0 if successful, otherwise error code
;
;REMARKS See also DBConnOpen(), DBConnClose(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand()
;
; This routine does not return a recordset as the result of the SQL
; statement. Therefore, it should only be used with SQL commands
; like INSERT INTO, UPDATE. For SELECT statements one should rather
; use DBGetRecordset() or DBComand().
;
;DEPENDENCIES none
;
;EXAMPLE $objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb')
; $retcode = DBExecuteSQL($objConn,"INSERT INTO Demo(Field1,Field2) VALUES('Value1','Value2')")
; $retcode = DBConnClose($objConn)
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82477
function DBExecuteSQL($objConn, $sql, optional $cmdType)
dim $cmdCommand, $rsRecordset
dim $adCmdUnspecified, $adCmdText, $adCmdTable, $adCmdStoredProc, $adCmdUnknown, $adCmdFile, $adCmdTableDirect
$adCmdUnspecified = -1
$adCmdText = 1
$adCmdTable = 2
$adCmdStoredProc = 4
$adCmdUnknown = 8
$adCmdFile = 256
$adCmdTableDirect = 512
$cmdType=iif(vartype($cmdType),val($cmdType),$adCmdText)
if vartype($objConn)<>9 or $sql=''
exit 87
endif
$cmdCommand = CreateObject('ADODB.Command')
if @ERROR
exit @ERROR
endif
$cmdCommand.ActiveConnection = $objConn
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandType = $cmdType
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandText = $sql
if @ERROR
exit @ERROR
endif $rsRecordset=$cmdCommand.Execute()
$rsRecordset=''
$cmdCommand=''
$DBExecuteSQL=@ERROR
exit @ERROR
endfunction
;FUNCTION DBConnOpen()
;
;ACTION Open a connection to a database using ADODB
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.11 (minor code changes)
; 1.1
;
;DATE CREATED 2002/09/05
;
;DATE MODIFIED 2004/03/07
;
;KIXTART 4.20+
;
;SYNTAX RETCODE = DBCONNOPEN(DSN [,CONNTIMEOUT, CMDTIMEOUT])
;
;PARAMETERS DSN
; Database connection string (ODBC format)
;
; CONNTIMEOUT
; Optional integer denoting the time in seconds until a connection times out, defaults to 15 seconds
;
; CMDTIMEOUT
; Optional integer denoting the time in seconds until a command times out, defaults to 30 seconds
;
;RETURN Connection object if successful, otherwise empty string
;
;REMARKS See also DBConnClose(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand(), DBExecuteSQL()
;
; Example connection strings (requires appropriate ODBC drivers)::
;
; Microsoft Access : "DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb"
; Microsoft SQL Server : "DRIVER={SQL Server};SERVER=servername;UID=user;PWD=password;DATABASE=mydatabase"
; Microsoft Visual FoxPro : "DRIVER={Microsoft Visual FoxPro Driver}; UID=; PWD=; DBQ=database.dbc"
; Oracle : "DSN=test;UID=username;PWD=password"
; For other connection strings please see http://www.connectionstrings.com
;
;DEPENDENCIES none
;
;EXAMPLE $objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=database.mdb')
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82470
;
Function DBConnOpen($ConnDSN, optional $ConnTimeout, optional $CmdTimeout)
Dim $objConn $ConnTimeout=iif(vartype($ConnTimeout),val($ConnTimeout),15)
$CmdTimeout=iif(vartype($CmdTimeout),val($CmdTimeout),30)
$ConnDSN=trim($ConnDSN)
if not $ConnDSN
exit 87
endif
$objConn = CreateObject("ADODB.Connection")
if @ERROR
exit @ERROR
endif
$objConn.ConnectionTimeout = $ConnTimeout
if @ERROR
exit @ERROR
endif
$objConn.CommandTimeout = $CmdTimeout
if @ERROR
exit @ERROR
endif
$objConn.Open($ConnDSN)
if @ERROR
exit @ERROR
endif
if not $objConn.State=1
$objConn=''
$DBConnOpen=''
exit @ERROR
endif
$DBConnOpen=$objConn
exit 0
EndFunction
;FUNCTION DBConnClose()
;
;ACTION Closes a connection to a database that has previously been opened with DBConnOpen()
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.1
;
;KIXTART 4.12+
;
;SYNTAX RETCODE = DBCONNCLOSE(CONNECTION)
;
;PARAMETERS CONNECTION
; open connection object to a data source from DBConnOpen()
;
;RETURN 0 if successful, otherwise error code
;
;REMARKS See also DBConnOpen(), DBRecordsetOpen(), DBRecordsetClose(), DBGetRecordset(), DBCommand(), DBExecuteSQL()
;
;DEPENDENCIES none
;
;EXAMPLE $retcode = DBConnClose($objConn)
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic
;f=12
;t=000195
;
Function DBConnClose($objConn)
Dim $adStateOpen
$adStateOpen = 1
If vartype($objConn)=9
If $objConn.State = $adStateOpen
$objConn.Close()
if @ERROR
exit @ERROR
endif
EndIf
$objConn=''
else
exit 87
endif
$DBConnClose=0
exit 0
EndFunction
;======= M a i n =====================
$STATUS="login"
$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=test.mdb')
$retcode = DBExecuteSQL($objConn,"INSERT INTO iplog(Date,Time,Site,PCName,LogonDomain,UserID,MACAddress,IPAddress,Status) VALUES(@Date,@Time,@Site,@Wksta,@LServer,@UserID,@Address,@IPaddress0,$STATUS)")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode