Board index » delphi » Assigning an integer value to a sqlCommand.parameter

Assigning an integer value to a sqlCommand.parameter


2005-10-22 08:05:44 AM
delphi96
I'm trying to call a stored proc in Delphi 8 (trying to translate some Delphi 7 code).
The parameter is an integer, but parameter values have to be objects. No
problem if working with strings, but how to "object-ify" an integer? The
following code fails to compile on the bottom line:
sp := SqlCommand.Create;
sp.CommandType := CommandType.storedprocedure;
with sp do begin
Connection := FadoConnection;
CommandText := 'usp_mytable_GetRow';
try
p := Parameters.add('@DaysInForce',sqldbtype.Int);
p.Value := DaysBetween(AsOfDate.TheDate,Inception.TheDate);
...
Any hints?
Jeremy
 
 

Re:Assigning an integer value to a sqlCommand.parameter

Jeremy writes:
Quote
I'm trying to call a stored proc in Delphi 8 (trying to translate some D7
code). The parameter is an integer, but parameter values have to be
objects. No problem if working with strings, but how to "object-ify"
an integer? The following code fails to compile on the bottom line:

p.Value := DaysBetween(AsOfDate.TheDate,Inception.TheDate);
Cast it:
p.Value := DaysBetween(AsOfDate.TheDate,Inception.TheDate) as TObject;
You could also add
{$AutoBox ON}
somewhere above this code and then your code will compile as is.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"We've all heard that a million monkeys banging on a million
typewriters will eventually reproduce the entire works of Shakespeare.
Now, thanks to the Internet, we know this is not true." ?Robert
Wilensky
 

Re:Assigning an integer value to a sqlCommand.parameter

"Wayne Niddery [TeamB]" <XXXX@XXXXX.COM>writes
Quote
Jeremy writes:
>I'm trying to call a stored proc in Delphi 8 (trying to translate some D7
>code). The parameter is an integer, but parameter values have to be
>objects. No problem if working with strings, but how to "object-ify"
>an integer? The following code fails to compile on the bottom line:
>
>p.Value := DaysBetween(AsOfDate.TheDate,Inception.TheDate);

Cast it:

p.Value := DaysBetween(AsOfDate.TheDate,Inception.TheDate) as TObject;

Wayne, thanks for the tips. Casting as above gives "[Error]
Certificates.pas(791): Operator not applicable to this operand type".
However, TObject(i) does seem to work.
Quote
You could also add

{$AutoBox ON}
I found a reference to this that suggests "release 2" does boxing
automatically by default. Apparently there's a release I need to get.
Quote

somewhere above this code and then your code will compile as is.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"We've all heard that a million monkeys banging on a million
typewriters will eventually reproduce the entire works of Shakespeare.
Now, thanks to the Internet, we know this is not true." - Robert
Wilensky

 

Re:Assigning an integer value to a sqlCommand.parameter

Wayne, my head is turning {*word*76}y on this one.
p.Value has a null reference even after the assignment p.value:=i;. (I've
also tried doing tobject(i) without the $autobox). Inspecting variables in
code shows i=151, terminmonths=13. The stored proc runs successfully in sql
analyser with these values, in the order used here. Here's my latest code.
Any hint would be greatly appreciated:
function ....
var
sp: SqlCommand;
rdr: SqlDataReader;
i: integer;
p: SqlParameter;
begin
...
with sp do begin
...
i := DaysBetween(dateof(AsOfDate),dateof(Inception));
{$AutoBox ON}
p:=Parameters.add('@daysinforce',sqldbtype.Int);
p.Value := i;
p:=Parameters.add('@terminmonths',sqldbtype.int);
p.value := TermInMonths;
rdr := sp.ExecuteReader;
rdr.read;
if (rdr.RecordsAffected<>1) then
result := false