如何在 Delphi 与 .NET Web Service 之间互相传输DataSet (2)

.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;

转载于:https://www.cnblogs.com/qianwt/archive/2006/08/14/476146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值