Page 1 of 1 1
Topic Options
#85118 - 2002-02-14 01:13 AM New to COM - COMments?
MarkR Offline
Lurker

Registered: 2002-02-14
Posts: 2
Loc: Golden, Colorado USA
Hi all,
I'm new to COM programming, and am trying to understand how to implement in KiXtart.

I'm attempting to attach to a SQL server, run a query, and return an integer value as the result of the query.

What works:
I successfully connect to the database:
$objConnect = CreateObject("ADODB.Connection")
$objConnect.Open("$CONNECTION_STRING")

I then run my query with a SQL statement:
$objRec = $objConnect.execute("SELECT COUNT(*) AS REC_COUNT FROM tablename WHERE field = 'N'")

What doesn't work:
$objRec should contain the integer result of the query, right? Wrong! it appears as a longish number (15239064 or some such) - not what I expected. An equivalent VB script returns a value of 4 or 5 for the same query.

Does anyone have insights on how I should get my integer value?

Thanks in advance!
MarkR

Top
#85119 - 2002-02-14 01:06 PM Re: New to COM - COMments?
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
I presume your integer value is supposed to be a record count. Is this correct?
_________________________
================================================
Breaker


Top
#85120 - 2002-02-14 01:41 PM Re: New to COM - COMments?
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
Just had a look through some of the MS Docs for ADO. When you run the Execute() method on a connection object, you return a recordset with only limited functionality.

You would be better off creating a recordset object first, running your SQL query without the COUNT function embedded, to simply return the results to the recordset object, then using the RecordCount() method to get an integer value representing the number of records. Here's an example from a script I wrote to write logon data to an Access database.

code:
$DATABASE = "\\corpfs01\rollout$$\logon.mdb"
$DSN="Driver={Microsoft Access Driver (*.mdb)}; DBQ=$DATABASE"

;Create SQL statements to check/write/update database
$CHECK_ENTRY_USERS = "SELECT * FROM TBL_USERS WHERE USERNAME='@USERID';"
$CHECK_ENTRY_COMPUTERS = "SELECT * FROM TBL_COMPUTERS WHERE WORKSTATION='@WKSTA';"

$Connection = CreateObject("ADODB.Connection")
$Command = CreateObject("ADODB.Command")
$Recordset = CreateObject("ADODB.Recordset")

;Check for connection object
if $Connection

;Open connection to database
$Connection.ConnectionString = $DSN
$Connection.Open()

;Check for existing records for the current workstation/user
;Add new record or update existing as required

;Check for User details first
$Command.ActiveConnection = $Connection
$Recordset.CursorType = 3
$Recordset.LockType = 3
$Recordset.ActiveCommand = $Command
$Command.CommandText = $CHECK_ENTRY_USERS
$Recordset.Open($Command)

;Create new record if none exists to update
If $Recordset.RecordCount < 1
$Recordset.AddNew
Endif



You can see how to create the recordset that will allow you to use the RecordCount() method here.

MS documentation on ADO can be found here with examples in VB and VBS for nearly all methods and properties.

HTH

_________________________
================================================
Breaker


Top
#85121 - 2002-02-14 02:57 PM Re: New to COM - COMments?
Alex.H Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 406
Loc: France
Guys,
Just ot get back to the first example,
$objRec is in fact an object, and the number you got is the memory address of this one

The VB script automaticaly launch this command when you called the object:
$objRec("field 1")
that's why you got the value

You'll need to do this explicitly with kixtart :
$objRec("REC_COUNT")

_________________________
? getobject(Kixtart.org.Signature)

Top
#85122 - 2002-02-14 11:34 PM Re: New to COM - COMments?
MarkR Offline
Lurker

Registered: 2002-02-14
Posts: 2
Loc: Golden, Colorado USA
Hi everyone,
Thanks for the help! I'm on the way to unscrewing the record count, and will post my solution when it is tested.
Cheers!
MarkR

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2312 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.055 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org