3

我有一个做财务报告的项目,我想让用户能够通过互联网获得这些报告

我尝试使用 TIdHTTPServer 这是一个 Indy 组件来使我的应用程序作为 HTTP 服务器工作并让它能够

接收请求 -> 处理请求 -> 发回请求处理的结果

使用特殊端口。

现在我的问题是我收到很多访问冲突错误和随机异常,这看起来像是线程问题,或者我不知道,因为如果我在不使用 TIdHTTPServer 的情况下处理相同的请求,我不会遇到任何问题

我正在使用 OnCommandGet 事件来处理请求并将结果发送回上下文流中的用户。

我需要的是演示如何将它与 TADODataSet 和 TADOConnection 一起使用

例如,我需要用户能够发送请求并且 TIdHTTPServer 接受请求(例如,使用 ADODataSet 调用存储过程并将结果作为 XML 文件并将其发送回用户)

请帮忙....谢谢。

4

1 回答 1

3

服务器如何工作的一种可能性......

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,IDContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer, StdCtrls, DB, ADODB;

type
  TForm3 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Memo1: TMemo;
    Button1: TButton;
    DummyConnection: TADOConnection;
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation
uses ComObj,AdoInt,ActiveX;
{$R *.dfm}
function SendStream(AContext: TIdContext; AStream: TStream): Boolean;
begin
   Result := False;
   try
     AContext.Connection.IOHandler.Write(AStream.Size);  // sending length of Stream first
     AContext.Connection.IOHandler.WriteBufferOpen;
     AContext.Connection.IOHandler.Write(AStream, AStream.Size);
     AContext.Connection.IOHandler.WriteBufferFlush;
   finally
     AContext.Connection.IOHandler.WriteBufferClose;
   end;
   Result := True;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  IdTCPServer1.Active := true;
end;


{ Clientside function
Function RecordsetFromXMLStream(Stream:TStream): _Recordset;
var
  RS: Variant;
begin
    RS := CreateOleObject('ADODB.Recordset');
    RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
    Result := IUnknown(RS) as _Recordset;
end;
}

Procedure RecordsetToXMLStream(const Recordset: _Recordset;Stream:TStream);
var
  RS: Variant;
begin
    if Recordset = nil then Exit;
    RS := CreateOleObject('ADODB.Recordset');
    RS := Recordset;
    RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
    Stream.Position := 0;
end;

Procedure GetQueryStream(Const s,ConStr:String;ms:TMemoryStream);
var
 AC:TAdoConnection;
 ads:TAdodataset;
begin
 AC:=TAdoConnection.Create(nil);
 try
 ads:=TAdodataset.Create(nil);
   try
     ads.Connection := AC;
     AC.ConnectionString := ConStr;
     ads.CommandText := s;
     ads.Open;
     RecordsetToXMLStream(ads.Recordset,ms);
   finally
     ads.Free
   end;
 finally
   AC.Free
 end;

end;

procedure TForm3.IdTCPServer1Execute(AContext: TIdContext);
var
 cmd:String;
 ms:TMemoryStream;
begin
     CoInitialize(nil);
     AContext.Connection.IOHandler.Readln(cmd);
     ms:=TMemoryStream.Create;
     try
     GetQueryStream('Select * from Adressen',DummyConnection.ConnectionString,ms);
     ms.Position := 0;
     SendStream(AContext,ms);
     AContext.Connection.Socket.CloseGracefully;
     finally
        ms.Free;
        CoUninitialize;
     end;

end;
end.
于 2012-11-20T10:18:46.363 回答