动态生成 TParameters ?

kmfangxun 2006-11-28 07:40:13
1、
TParameters *Params = new TParameters( this,__classid(TParameter) );

Params->Add();
Params->Items[0]->Value=strName;//出错,提示 invalid class type cast

//////////////////////////////////

2、
TParams *Params = new TParams();
Params->Add();
Params->Items[0]->Value=strName;//顺利通过

好奇怪啊!

...全文
247 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmfangxun 2006-11-30
  • 打赏
  • 举报
回复

非常感谢!
kmfangxun 2006-11-29
  • 打赏
  • 举报
回复
to: ccrun(老妖)(www.ccrun.com)

TParameters确实只用于ADO,我是看看能不能动态生成TParameters 。

用TParameters *Params=AdoQuery->Parameters一点问题也没有。

但是用 TParameters *Params = new TParameters( this,__classid(TParameter) );却不行。
ccrun.com 2006-11-29
  • 打赏
  • 举报
回复
TParameters好象仅用于ADO中吧。
kmfangxun 2006-11-29
  • 打赏
  • 举报
回复
to: BenLeak(摇摆人)

bcb6


to: jjwwang((空园歌独酌,春日赋闲居))


this是TPersistent继承类实列,我用的是TForm的实列。

strName 是字符串。指定类型和大小也没有用。









CACACACACA 2006-11-29
  • 打赏
  • 举报
回复
说的有点乱.请将就着盾吧.
CACACACACA 2006-11-29
  • 打赏
  • 举报
回复
因为TParameter只是一个"描述",并不是真的ADO中的对像.

(2) 、没报错
TParameters *ADOParams = ADODataSet1->Parameters;
ADOParams->AddParameter();
ADOParams->Items[0]->Value="4354"; //正确
ADOParams->AddParameter();
ADOParams->Items[1]->Value="aaaaa";
在(2)中. Command建立了. 有_Parameter对象了.
而赋值最终是给_Parameter赋值.
所以不会报错.
CACACACACA 2006-11-29
  • 打赏
  • 举报
回复
再详细的说起来就麻烦了.我简单的解释一下.(是我自己的更解)
constructor TCustomADODataSet.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCommand := TADOCommand.Create(Self); //内部构造了TADOCommand对象
FCommand.ComponentRef := Self;
FIndexDefs := TIndexDefs.Create(Self);
FModifiedFields := TList.Create;
FIndexFields := TList.Create;
FCursorType := ctKeyset;
FLockType := ltOptimistic;
FCursorLocation := clUseClient;
FCacheSize := 1;
CommandType := cmdText;
NestedDataSetClass := TADODataSet;
FMasterDataLink := TMasterDataLink.Create(Self);
MasterDataLink.OnMasterChange := MasterChanged;
MasterDataLink.OnMasterDisable := MasterDisabled;
EnableBCD := True;
end;

而TCustomADODataSet的"子类TADODataSet"又公布了Parameters属性 ->( property Parameters )

//下面是对这个属性的"读取"
function TCustomADODataSet.GetParameters: TParameters;
begin
//这里的Command已经是构造好的.
Result := Command.Parameters;
end;


TParameter = class(TCollectionItem)
private
FParameter: _Parameter; //注意这
function GetAttributes: TParameterAttributes;
function GetDataType: TDataType;
function GetName: WideString;
function GetNumericScale: Byte;
function GetParameter: _Parameter; //注意这
function GetParameterDirection: TParameterDirection;
function GetPrecision: Byte;
function GetProperties: Properties;
function GetSize: Integer;
function GetValue: Variant;
procedure SetAttributes(const Value: TParameterAttributes);
procedure SetDataType(const Value: TDataType);
procedure SetName(const Value: WideString);
procedure SetNumericScale(const Value: Byte);
procedure SetParameterDirection(const Value: TParameterDirection);
procedure SetPrecision(const Value: Byte);
procedure SetSize(const Value: Integer);
procedure SetValue(const Value: Variant);
function GetParameters: TParameters;
protected
procedure AssignTo(Dest: TPersistent); override;
function GetDisplayName: string; override;
function IsEqual(Value: TParameter): Boolean;
public
procedure Assign(Source: TPersistent); override;
procedure AppendChunk(Val: OleVariant);
procedure LoadFromFile(const FileName: string; DataType: TDataType);
procedure LoadFromStream(Stream: TStream; DataType: TDataType);
property ParameterObject: _Parameter read GetParameter; //注意这
property Parameters: TParameters read GetParameters;
property Properties: Properties read GetProperties;
published

最终还是要在这里真正的建立.TParameter只是一个"描述"罢了
function TParameters.CreateParameter(const Name: WideString;
DataType: TDataType; Direction: TParameterDirection; Size: Integer;
Value: OleVariant): TParameter;
begin
Result := AddParameter;
Result.FParameter := Create_Parameter(Name, DataType, Direction, Size);//下面例出了这个方法
{ Don't try to assign value when it is an EmptyParam (used when optional) }
if not ((TVarData(Value).VType = varError) and
(TVarData(EmptyParam).VError = $80020004)) then
Result.FParameter.Value := Value;
end;


function TParameters.Create_Parameter(const Name: WideString;
DataType: TDataType; Direction: TParameterDirection = pdInput;
Size: Integer = 0): _Parameter;
const
ValidDirectionValues: array[TParameterDirection] of TOleEnum =
(adParamInput, adParamInput, adParamOutput, adParamInputOutput,
adParamReturnValue);
begin
Result := Command.CommandObject.CreateParameter(Name, DataTypeValues[DataType],
ValidDirectionValues[Direction], Size, Null);
end;
kmfangxun 2006-11-29
  • 打赏
  • 举报
回复
to: jjwwang((空园歌独酌,春日赋闲居))

谢谢,明白了一点,可是为什么下面的(2)同样是生成 TParameter 却没有报错?


(1) 、报错
TParameters *ADOParams = new TParameters( ADODataSet1, __classid(TParameter) );
ADOParams->AddParameter();
ADOParams->Items[0]->Value="4354"; //报错 invalid class type cast
ADOParams->AddParameter();
ADOParams->Items[1]->Value="aaaaa";

//-------------------------------------------------------------
(2) 、没报错
TParameters *ADOParams = ADODataSet1->Parameters;
ADOParams->AddParameter();
ADOParams->Items[0]->Value="4354"; //正确
ADOParams->AddParameter();
ADOParams->Items[1]->Value="aaaaa";



CACACACACA 2006-11-29
  • 打赏
  • 举报
回复
另外:

TParameters *ADOParams = new TParameters( ADODataSet1/*this*/, __classid(TParameter) );
ADOParams->AddParameter();
ADOParams->AddParameter();
Caption = IntToStr( ADOParams->Count ); //显示 2

这是没能问题的.所以是上面的问题
即 ParameterObject.Value := NewValue; 会报错
CACACACACA 2006-11-29
  • 打赏
  • 举报
回复
procedure TParameter.SetValue(const Value: Variant);
const
SizedDataTypes = [ftUnknown, ftString, ftFixedChar, ftWideString, ftMemo,
ftBlob, ftBytes, ftVarBytes];
var
NewSize: Integer;
NewValue: OleVariant;
begin
if VarIsClear(Value) or VarIsNull(Value) then
NewValue := Null
else
begin
if DataType = ftUnknown then
SetDataType(VarTypeToDataType(VarType(Value)));
{ Convert blob data stored in AnsiStrings into variant arrays first }
if (DataType = ftBlob) and (VarType(Value) = varString) then
NewValue := StringToVarArray(Value) else
NewValue := Value;
end;
if DataType in SizedDataTypes then
begin
NewSize := VarDataSize(NewValue);
if (Size = 0) or (NewSize > Size) then
Size := NewSize;
end;
ParameterObject.Value := NewValue;
end;

看最后一行
ParameterObject.Value := NewValue;
再看
property ParameterObject: _Parameter read GetParameter;
赋值的时候当然会报错了.
CACACACACA 2006-11-28
  • 打赏
  • 举报
回复
另外
指定 Params->Items[0]的类型和size.
CACACACACA 2006-11-28
  • 打赏
  • 举报
回复
1, TParameters *Params = new TParameters( this,__classid(TParameter) );
里的this是什么

2. strName是什么类型的, 是不是这转换出错了
Represents the value of the parameter as a Variant.

Delphi syntax:

property Value: OleVariant;

C++ syntax:

__property Variant Value = {read=GetValue, write=SetValue};

BenLeak 2006-11-28
  • 打赏
  • 举报
回复
BCB 5 or 6?

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧