2012-11-20 37 views
3

我有一個項目,做財務報告,我想,讓用戶能夠得到這個報告通過互聯網大廈HTTP服務器應用

我嘗試使用TIdHTTPServer這是一個印組件,使我的應用程序工作作爲一個HTTP服務器,並讓它能夠

收到請求 - >處理請求 - >發送回使用一個特殊的端口請求過程

的結果。

我現在的問題是,我得到了很多的訪問衝突誤差和隨機異常 它看起來像有關線程的問題,或者我不知道,因爲如果我處理相同的請求不使用TIdHTTPServer我不得到任何問題

我使用OnCommandGet事件來處理請求並將結果發送回上下文流中的用戶。

我需要的是如何與TADODataSet和TADOConnection

比如我需要的用戶可以發送一個請求,TIdHTTPServer接受請求使用它的演示(例如調用存儲過程使用到ADODataSet並將結果作爲XML文件發回給用戶)

請幫忙....謝謝。

回答

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.