如何显示从其它单元传递过来的Variant值呀??非高手勿进!!(50分)

  • 主题发起人 主题发起人 microrain
  • 开始时间 开始时间
M

microrain

Unregistered / Unconfirmed
GUEST, unregistred user!
是这样的,我在其它单元操作数据库,获得了记录集,然后传递给一个Variant变量。然后
在调用单元中获取这个变量。如何再显示出这个变量中的记录???
 
使用的时候能不能这么用a[0]a[1]....
 
是这样的,我现在在做一个封装ADO操作的类。然后其它的窗体中只要用到数据库操作的地
方只要调用一下这个单元中的相关操作就行了。
比如我有一个窗口要查询数据库,调用那个封装了ADO操作的单元。然后将得到的结果反回到
当前的窗口中来。在当前的窗口只要将所执行SQL语句传给那个单元中的相关方法就可以了。
不知道你明白我所说的了吗?
 
大概明白了
不过我以前用的时候都是把这个变量定义为tstrings,这样有缺陷就是不能返回非字符串型的
结果
变体类型不知道是否支持元素的枚举呢
 
你可以用VarArray数组,
V: Variant;
...
V := VarArrayCreate([0,9], varInteger);//建立数组

这是VarArray的一些类型.VarType
const
varEmpty = $0000;
varNull = $0001;
varSmallint = $0002;
varInteger = $0003;
varSingle = $0004;
varDouble = $0005;
varCurrency = $0006;
varDate = $0007;
varOleStr = $0008;
varDispatch = $0009;
varError = $000A;
varBoolean = $000B;
varVariant = $000C;
varUnknown = $000D;
varShortInt = $0010;
varByte = $0011;
varWord = $0012;
varLongWord = $0013;
varInt64 = $0014;
varStrArg = $0048;
varString = $0100;
varAny = $0101

varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;

variant是什么类型,你可以用VarType来判断.
VarType Contents of variant

varEmpty The variant is Unassigned.
varNull The variant is Null.
varSmallint 16-bit signed integer (type Smallint).
varInteger 32-bit signed integer (type Integer).
varSingle Single-precision floating-point value (type Single).
varDouble Double-precision floating-point value (type Double).
varCurrency Currency floating-point value (type Currency).
varDate Date and time value (type TDateTime).
varOleStr Reference to a dynamically allocated UNICODE string.

varDispatch Reference to an Automation object (an IDispatch interface pointer).
varError Operating system error code.
varBoolean 16-bit boolean (type WordBool).
varVariant A variant.
varUnknown Reference to an unknown OLE object (an IInterface or IUnknown interface pointer).
varShortInt 8-bit signed integer (type ShortInt)
varByte A Byte
varWord unsigned 16-bit value (Word)
varLongWord unsigned 32-bit value (LongWord)
varInt64 64-bit signed integer (Int64)

varStrArg COM-compatible string.
varString Reference to a dynamically allocated string (not COM compatible).
varAny A CORBA Any value.

另外,如果你用三层,TClientDataSet.Data本身就是OleVariant,可以传递数据集,建议你看
看.如果你是用两层,你可以用VarArrayCreate建立数组,数组中可以保存各种类型的值,我记
得以前有相关的答案,你要不要搜一下?
算了,我帮你搜到两个,你看看吧.
452396,685181.
 
好的,我去看一看。各位,帮帮我吧。我的这个项目合指这个呢,我又对delphi不熟。惨呀
希望大家能帮帮我。
 
呵呵,这是我程序里的一段——
var
tempquery:TAdoQuery;
lookupres:variant;
begin
tempquery:=Tadoquery.Create(nil);
tempquery.Connection:=mainfrm.acomain;
tempquery.SQL.Text:=format('select * from jgzss where xphid=''%s''',[dixphid.Inspector.InplaceEditor.GetEditingText])
tempquery.Open;
lookupres:=tempquery.Lookup('xhid;jgfl;xphid',
VarArrayOf([dixhid.edittext,dijgfl.edittext,
dixphid.Inspector.InplaceEditor.GetEditingText]),
'dj;yx;yz');
if not VarIsArray(lookupres) then
begin
showmessage('加工指示书中不存在此大批号,请验证!');
end
else
begin
if not varisnull(lookupres[0]) then
didj.EditText:=lookupres[0]
else
didj.EditText:='';
if not varisnull(lookupres[1]) then
diyx.EditText:=lookupres[1]
else
diyx.EditText:='';
if not varisnull(lookupres[2]) then
diyz.EditText:=lookupres[2]
else
diyz.EditText:='';
end;
tempquery.Close;
tempquery.Free;
end;
 
呵呵,楼上的与我所说的差不多呀。这样吧我将代码帖出来,请大家帮我看一看


下面的是我要封装ADO操作的单元文件。

unit Eado;

interface
uses
Windows, Messages, SysUtils,ADODB_TLB, ComObj,Forms,Dialogs;
type
TEAdo=class
private

objConn:OleVariant;
RecordSet:OleVariant;
spath:String;
Stest:String;
function opendb:Boolean;
procedure closedb;
public
RecordSet2:OleVariant;
constructor Create;
//destructor Destroy;
function AddRecord(StrSQL:String):Boolean;
function DelRecord(nIndex:integer):Boolean;
function getValue:String;
function getRecord:OleVariant;

end;

implementation

constructor TEAdo.Create;
begin
objConn:=CreateOleObject('ADODB.Connection');
RecordSet:=CreateOleObject('ADODB.Recordset');
end;

{* 对数据库进行初始化并打开数据库 *}
Function TEAdo.opendb:Boolean;
begin
try
spath:=extractfilepath(application.ExeName )+'data/clientdb.mdb';
objConn.open('driver={Microsoft Access Driver (*.mdb)};dbq='+spath);
result:=true;
finally
result:=false;
end;
end;

{* 关闭数据库连接 *}
Procedure TEAdo.closedb;
begin
objConn.close;
end;

function TEAdo.AddRecord(StrSQL:String):Boolean;
var
SQLtxt:string;
begin
opendb;
SQLtxt:=strSQL;
RecordSet2:=RecordSet.Open(SQLtxt,objConn,3,3);
RecordSet.Close;
closedb;
result:=true;
end;
function TEAdo.getValue:String;
begin
result:=stest;
end;
function TEAdo.getRecord:OleVariant;
begin
result:= RecordSet2;
end;
function TEAdo.DelRecord(nIndex:integer):Boolean;
begin
opendb;
result:=true;
end;
end.


下面的是调用文件
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Eado, StdCtrls,ComObj;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure Button2Click(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
MyAdo:TEAdo;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
MyAdo:=TEAdo.Create;
end;

procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
myado.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
RecordSet:Variant;
data:string;
begin

if not myado.AddRecord('select * from commandlist') then
begin
ShowMessage('error');
end;
recordset:=myado.RecordSet2;
//在这里想得到recordset取得的记录集,,,各位帮忙呀
//如何显示出记录的内容呀
end;


end.
 
单元1,下面函数用于取得Order表中的ID,Date等字段到Variant变量。
Function TDMOrder.GetOrderMasterData:Variant;
var
FromDate, ToDate: TDate;
i: Integer;
begin
FromDate := EnCodeDate(FromYear, FromMonth, 1);
ToDate := GetYearMonthLastDate(ToYear, ToMonth);//this function declare in datamodule's parent
with qryOrder do
begin
Close;
Parameters.ParamByName('FromDate').Value := FromDate;
Parameters.ParamByName('ToDate').Value := ToDate;
Open;
i := 1;
Result := VarArrayCreate([1, RecordCount, 0,4], varOleStr);//详见VarArrayCreate帮助,以悉VarOleStr等的作用
While Not Eof do
begin
Result[i, 0] := FieldByName('ID').AsString;
Result[i, 1] := DateToStr(FieldByName('InDate').AsDateTime);
// Result[i, 1] := FieldByName('InDate').AsString;
Result[i, 2] := FieldByName('OurStyleNo').AsString;
Result[i, 3] := FieldByName('Remark').AsString;
Inc(i);
Next;
end;
end;
end;

下面函数用于调用刚才的函数,获得Variant变量,并显示到StringGrid中,
上面的函数我是做为一个COM对象里的一个方法的,所以下面的函数建立了一个COM对象
至于COM对象的做法,不在这里讨论。
procedure TfrmOrder.GetOrderData;
var
FromYear, FromMOnth,
ToYear, ToMonth: Word;
OrderData: OleVariant;
Order: IOrder;
i, OrderCount: integer;
begin
FromYear := StrToInt(framYearMonth1.cmbYear.Text);
FromMonth := StrToInt(framYearMonth1.cmbMonth.Text);
ToYear := StrToInt(framYearMonth2.cmbYear.Text);
ToMonth := StrToInt(framYearMonth2.cmbMonth.Text);
Order := CoOrder.Create;
Order.GetOrderData(FromYear, FromMOnth, ToYear, ToMonth, OrderData);
if VarIsNull(OrderData) then Exit;
//ini StringGrid if没有记录查到
sgOrder.RowCount := 2;
sgOrder.Rows[1].Clear;
OrderCount := VarArrayHighBound(OrderData, 1);//此处用于获得数组的个籽,看VarArrayHighBound方法以悉其用法。注意里面用1的目的。
if OrderCount <= 0 then Exit;
sgOrder.RowCount := OrderCount + 1;
for i:= 1 to OrderCount do
with sgOrder do
begin
Rows.Clear;
Rows.Add(OrderData[i, 0]);//逐个取得数组中的值,放中Cell
Rows.Add(OrderData[i, 1]);
Rows.Add(OrderData[i, 2]);
Rows.Add(OrderData[i, 3]);
end;
//focus到上次选择的那条裁单记录
if sgOrder.RowCount - 1 >= RowSelected then
sgOrder.Row := RowSelected;
end;
 
楼上代码也有,原理也有,这方面我就不多说了.
既然是你的recordset是OleVariant,那么你就把它当成RecordSet对象使用就是了.
但是我总觉得如果自己封装ADO类的操作,就应该更彻底一些.你应该对RecordSet
进行进一步的封装(如果只是提供RecordSet给别的调用用户的话,好像封装的意义
就不大了).
下面是ADO Object Model:

Connection
| —— Errors —— Error
| —— Properties —— Property

Command
| —— Parameters —— Parameter
| —— Properties —— Property

RecordSet
| —— Fields —— Field
| —— Properties —— Property

Record
| —— Fields —— Field

Stream

如果时间紧,你可以先将
RecordSet
| —— Fields —— Field
| —— Properties —— Property
封装好,这里面的技术,我觉得和Delphi无关.关键是你想让别人怎么用你所封装的数据集.
 
呵呵,这样行不行?
procedure TForm1.Button2Click(Sender: TObject);
var
RecordSet:Variant;
data:string;
begin

if not myado.AddRecord('select * from commandlist') then
begin
ShowMessage('error');
end;
recordset:=myado.RecordSet2
//-->>把这个myado.recordset2赋给DataSource的DataSet属性,
// 然后这个DataSource跟一个DbGrid相连,DbGrid用来显示。
//在这里想得到recordset取得的记录集,,,各位帮忙呀
//如何显示出记录的内容呀
end;
 
这种方式不行呀。能不能写全一点试一试,好吗
 
搞什么搞,我不是高手,不能进来?
[:(][:(][:(]
 
按照C++来说,Dataset是一块内存,传一个指针就可以了,或者整个内存拷贝试试。
 
后退
顶部