#177464 - 2007-07-03 06:07 AM
use KIX to ACCESS database
|
bbs2me
Lurker
Registered: 2007-06-28
Posts: 3
|
Hello,
I would like to use the kix to put some information to a access database. I just copy the function DBExecuteSQL, DBConnOpen and DBConnClose that I found in UDF Library. But when I run it, I got an error: -2147352567.
Anyone can help me? Below is my source code for ref. Thank you.
;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
Edited by Richard H. (2007-07-03 05:56 PM) Edit Reason: Added code tags
|
|
Top
|
|
|
|
#177483 - 2007-07-03 10:32 AM
Re: use KIX to ACCESS database
[Re: Arend_]
|
bbs2me
Lurker
Registered: 2007-06-28
Posts: 3
|
What do you mean about use the [ code ] [ /code ] tags in my code. only the Main in write by me. The other function is found in UDF library.
Thank you.
Anyway, I have try to use the real data in the main program. But I still appear error.
;======= M a i n ============
$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('2007/07/03','12:00:20','test','DEMO','\\DEMO','test','000E35B6E1FF','172.27.0.66','login')")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode
;==================================
Edited by Richard H. (2007-07-03 05:54 PM) Edit Reason: Added CODE tags
|
|
Top
|
|
|
|
#177547 - 2007-07-04 01:45 AM
Re: use KIX to ACCESS database
[Re: Mart]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4574
Loc: USA
|
Try this... (make sure to update the path in $DB)
;======= M a i n ============
$DB="Path\test.mdb"
$objConn = DBConnOpen('DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=' + $DB)
? @serror
$retcode = DBExecuteSQL($objConn,"INSERT INTO iplog(Date,Time,Site,PCName,LogonDomain,UserID,MACAddress,IPAddress,Status) VALUES('2007/07/03','12:00:20','test','DEMO','\\DEMO','test','000E35B6E1FF','172.27.0.66','login')")
? $retcode
$retcode = DBConnClose($objConn)
? $retcode
;==================================
_________________________
(... better days ahead)
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 857 anonymous users online.
|
|
|