.NET Web Service 中 DataSet 参数传输时, 是把 DataSet 转换成 XML 格式的串进行传输,
这样的弊端是, 数据传输过大, 还有的就别的语言写的程序很难调用
本人在一个项目过程中, 需要.NET 写 Web Service, 而客户端需要Delphi来实现
鉴于这个原因, 我通过把DataTable转换成文本串的方法,
在上一篇文章中实现如何在 .NET 中如何对DataTable和文本串的互相转换,
这篇文章中将说明如何在Delphi中如何把DataSet和文本串的互相转换,
这样由于文本串的格式一致, 这样就可以在Delphi和.NET Web Service之间互相传输DataSet了
在Delphi中把DataSet转换成文本串, 遗憾的下面这个方法对字符串处理跟.NET中处理的性能不能比
可能在Delphi对字符串的处理还有更好的方法, 可我不知道, 目前只能如此了
function DataSetToString(DataSet: TADOQuery): string;
var
I: Integer;
SavePlace: TBookMark;
ss: TStringStream;
begin
ss := TStringStream.Create('');
with DataSet do
begin
SavePlace := GetBookmark;
DisableControls;
//生成列名称
for I := 0 to FieldCount - 1 do
begin
if I <> 0 then
ss.WriteString(',');
ss.WriteString('"' + Fields[I].FieldName + '"');
end;
//循环生成行
First;
while not Eof do
begin
for I := 0 to FieldCount - 1 do
begin
if I <> 0 then
ss.WriteString(',')
else
ss.WriteString(#13#10);
if not Fields[I].IsNull then
begin
if Fields[I] is TStringField then
ss.WriteString('"' + StringReplace(Fields[I].AsString,
'"', '""', [rfReplaceAll, rfIgnoreCase]) + '"')
else
ss.WriteString(Fields[I].AsString);
end;
end;
Next;
end;
GotoBookmark(SavePlace);
EnableControls;
end;
Result := ss.DataString;
ss.Free;
end;
生成随机字符串
function GetRandomFileName(intLen: Integer): string;
var
I: Integer;
const
ch = 'abcdefghijklmnopqrstuvwxyz0123456789';
begin
Randomize;
Setlength(Result, intLen);
for I := 1 to intLen do
begin
Result[I] := ch[Random(Length(ch)) + 1];
end;
end;
把文本串转换成DataSet
procedure StringToDataSet(DataSet: TADOQuery; const strDataSet: string);
var
strPath: string;
strList: TStringList;
strTempFileName: string;
strTempFullName: string;
begin
strPath := ExtractFilePath(ParamStr(0));
while true do
begin
strTempFileName := GetRandomFileName(15);
strTempFullName := strPath + strTempFileName + '.txt';
if not FileExists(strTempFullName) then
break;
end;
strList := TStringList.Create;
try
strList.Text := strDataSet;
strList.SaveToFile(strTempFullName);
DataSet.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Extended Properties=Text;Persist Security Info=False;' +
'Data Source=' + strPath;
DataSet.SQL.Text := 'select * from ' + strTempFileName + '#txt';
DataSet.Open;
finally
strList.Free;
DeleteFile(strTempFullName);
end;
end;
把文本串存到数据库中的临时表(此表是实际存在的, 只能用过后, 通过SQL删除掉)中去
procedure StringToTemp(DataSet: TADOQuery; const strDataSet, strTableName: string);
var
strPath: string;
strList: TStringList;
strTempFileName: string;
strTempFullName: string;
begin
strPath := ExtractFilePath(ParamStr(0));
while true do
begin
strTempFileName := GetRandomFileName(15);
strTempFullName := strPath + strTempFileName + '.txt';
if not FileExists(strTempFullName) then
break;
end;
ExecSQL('exec ProcDropTempTable ''' + strTableName + '''');
strList := TStringList.Create;
try
strList.Text := strDataSet;
strList.SaveToFile(strTempFullName);
DataSet.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Extended Properties=Text;Persist Security Info=False;' +
'Data Source=' + strPath;
DataSet.SQL.Text := Format('select * into %s_temp IN [OBBC][ODBC;Driver=' +
'SQL Server;UID=%s;PWD=%s;Server=%s;DataBase=%s%d;] from %s#txt',
[strTableName, Ini.ReadString('Server', 'User', 'sa'),
GetPass(Ini.ReadString('Server', 'Pass', SetPass('sntc*1234*'))),
Ini.ReadString('Server', 'Name', strHostName + '\CAANEWDB'),
GUID, DATA_YEAR, strTempFileName]);
DataSet.ExecSQL;
finally
strList.Free;
DeleteFile(strTempFullName);
end;
end;