L
lajfox
Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi封装了一个Com+接口,其中有两个函数:
1.GetDatasetData(TableName:WideString;out Rows:WideString):long;
2.SaveDataToTable(Rows,TableName:WideString):long;
我这样实现:
函数1.用ADO连接,用ADO.AdoRecord的Save方法把数据结果集写入一个流,把流转化为
string(delphi的TStringStream流无法返回WideString字符串)字符串。
函数2.接收函数1的Rows,把它转成流,用ADO.AdoRecord的Open方法读入。
现在我遇到的问题就是String和WideString转换的问题,如果rows是string类型则一切正常,
但要命的是接口中只能用WideString,我不知如何解决这个问题,敬请高手帮忙指点,非常着急。
下面是两个中间关键函数的代码:
{
Example:
...
Memo1.Lines.Text:=RecordsetToXML(ADOQuery1.Recordset);
...
}
function RecordsetToXML(const Recordset: _Recordset): String;
var RS: Variant;
Stream: TStringStream;
begin
Result:='';
if Recordset=nil then Exit;
Stream:=TStringStream.Create('');
try
RS:=CreateOleObject('ADODB.Recordset');
RS:=Recordset;
RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
Stream.Position:=0;
Result:=Stream.DataString;
finally
Stream.Free;
end;
end;
{
Example:
...
ADOQuery1.Recordset:=RecordsetFromXML(Memo1.Lines.Text);
...
}
function RecordsetFromXML(const XML: String): _Recordset;
var RS: Variant;
Stream: TStringStream;
begin
Result:=nil;
if XML='' then Exit;
try
Stream:=TStringStream.Create(XML);
Stream.Position:=0;
RS:=CreateOleObject('ADODB.Recordset');
RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
Result:=IUnknown(RS) as _Recordset;
finally
Stream.Free;
end;
end;
1.GetDatasetData(TableName:WideString;out Rows:WideString):long;
2.SaveDataToTable(Rows,TableName:WideString):long;
我这样实现:
函数1.用ADO连接,用ADO.AdoRecord的Save方法把数据结果集写入一个流,把流转化为
string(delphi的TStringStream流无法返回WideString字符串)字符串。
函数2.接收函数1的Rows,把它转成流,用ADO.AdoRecord的Open方法读入。
现在我遇到的问题就是String和WideString转换的问题,如果rows是string类型则一切正常,
但要命的是接口中只能用WideString,我不知如何解决这个问题,敬请高手帮忙指点,非常着急。
下面是两个中间关键函数的代码:
{
Example:
...
Memo1.Lines.Text:=RecordsetToXML(ADOQuery1.Recordset);
...
}
function RecordsetToXML(const Recordset: _Recordset): String;
var RS: Variant;
Stream: TStringStream;
begin
Result:='';
if Recordset=nil then Exit;
Stream:=TStringStream.Create('');
try
RS:=CreateOleObject('ADODB.Recordset');
RS:=Recordset;
RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
Stream.Position:=0;
Result:=Stream.DataString;
finally
Stream.Free;
end;
end;
{
Example:
...
ADOQuery1.Recordset:=RecordsetFromXML(Memo1.Lines.Text);
...
}
function RecordsetFromXML(const XML: String): _Recordset;
var RS: Variant;
Stream: TStringStream;
begin
Result:=nil;
if XML='' then Exit;
try
Stream:=TStringStream.Create(XML);
Stream.Position:=0;
RS:=CreateOleObject('ADODB.Recordset');
RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
Result:=IUnknown(RS) as _Recordset;
finally
Stream.Free;
end;
end;