ASP数据库数据表动态生成组件原码:unit main;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
  ComObj, ActiveX, AspTlb, emydb_TLB, StdVcl,ADODB_TLB;type
  Ttable = class(TASPObject, Itable)
  public
  protected
    procedure OnEndPage; safecall;
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    procedure clear(cls: OleVariant); safecall;
    procedure html(header, footer: OleVariant); safecall;
    procedure table(thstr, trstr, tdstr: OleVariant); safecall;
    procedure about; safecall;
    procedure error; safecall;
    procedure active; safecall;
    procedure dbopen(sqlstr, connstr: OleVariant); safecall;
    private
    FSQLSTR,FCONNSTR,Fheader,Ffooter,Fthstr,Ftrstr,Ftdstr:string;
    FTABLE,Ferror:string;
    isopen:boolean;
    FTR,FTH,FTD:STRING;
    myrs:TRecordset;
  end;implementationuses ComServ;procedure Ttable.OnEndPage;
begin
  inherited OnEndPage;
end;procedure Ttable.OnStartPage(const AScriptingContext: IUnknown);
begin
  inherited OnStartPage(AScriptingContext);
Fheader:='';
Ffooter:='';
Fthstr:='';
Ftrstr:='';
Ftdstr:='';
FTR:='';
FTH:='';
FTD:='';
myRs:=TRecordset.Create(nil);
end;procedure Ttable.clear(cls: OleVariant);
begin
if string(cls)='HTML' then
begin
Fheader:='';
Ffooter:='';
end;
if string(cls)='TABLE' then
begin
Fthstr:='';
Ftrstr:='';
Ftdstr:='';
end;
if string(cls)='1' then
begin
Fheader:='';
Ffooter:='';
end;
if string(cls)='2' then
begin
Fthstr:='';
Ftrstr:='';
Ftdstr:='';
end;if string(cls)='0' then
beginFheader:='';
Ffooter:='';
Fthstr:='';
Ftrstr:='';
Ftdstr:='';
end;
if string(cls)='ALL' then
beginFheader:='';
Ffooter:='';
Fthstr:='';
Ftrstr:='';
Ftdstr:='';
end;
end;procedure Ttable.html(header, footer: OleVariant);
begin
Fheader:= string(Fheader);
Ffooter:= string(footer);
end;procedure Ttable.table(thstr, trstr, tdstr: OleVariant);
begin
Fthstr:=string(thstr);
Ftrstr:=string(trstr);
Ftdstr:=string(tdstr);
end;procedure Ttable.about;
begin
response.Write('<center><B>程序:wnhoo</B><br>MAILTO:[email protected]</CENTER><HR>');
end;procedure Ttable.error;
beginend;procedure Ttable.active;
var
i:integer;
begin
IF ISOPEN THEN
begin
FTH:='';
for i:=0 to  myrs.Fields.Count-1 do  FTH:=FTH+'<TD>'+STRING(myrs.Fields.Item[I].Name)+'</TD>';
fth:='<tr>'+fth+'</tr>';
while not myrs.EOF  DO
BEGIN
FTD:='';
for i:=0 to  myrs.Fields.Count-1 do  FTD:=FTD+'<TD'+FTDSTR+'>'+STRING(myrs.Fields.Item[I].Value)+'</TD>';
FTR:=FTR+'<tr'+FTRSTR+'>'+FTD+'</tr>';
myrs.MoveNext;
end;myrs.Close;
FTABLE:='<TABLE border="1">'+fth+ftr+'</TABLE>';
response.Write(FTABLE)
end;
end;procedure Ttable.dbopen(sqlstr, connstr: OleVariant);
begin
Fsqlstr:=string(sqlstr);
Fconnstr:=string(connstr);
try
MYRS.Open(fsqlstr,fconnstr,2,3,0);
isopen:=true;
except
isopen:=false;
response.Write('数据库连接失败!<hr>');
end;
end;initialization
  TAutoObjectFactory.Create(ComServer, Ttable, Class_table,
    ciMultiInstance, tmApartment);
end.

解决方案 »

  1.   

    不错!不过俺没有环境!
    :)
    ASP中的ActiveX服务器组件为标准的Automation ActiveX组件,只要使用由ASP提供的接口和遵守ASP有关规则便可。Delphi 4.0作为一种高效、快速、强大的开发语言,为开发COM组件提供了很强的功能,但不知道什么原因,很少有人运用Delphi编写ASP中的ActiveX服务器组件。本文意在通过举例介绍如何运用Delphi编写ASP中的ActiveX 组件。
      通过以下步骤创建ASP中的ActiveX 服务器组件:
      1.创建一个ActiveX library工程
      打开Delphi编辑器,选择选单中的File/New,在New Item中选择 the ActiveX 项中的ActiveX Library选项,Delphi将自动生成以下代码:
      library Project1;
      uses
       ComServ;
      exports
       DllGetClassObject,
       DllCanUnloadNow,
       DllRegisterServer,
       DllUnregisterServer;
      {$R *.RES}
      begin
      end.
      这是Delphi编译COM组件时必须的函数,DllGetClassObject函数负责将输入调用从COM库转换到相应的类工厂中;DllCanUnloadNow函数是通知OLE引擎如果没有程序引用将自动释放内存;DllRegisterServer与DllUnregisterServer函数用来登记COM服务器。总之Delphi已经为你做好了一切辅助工作,你只需用心编写主要功能模块便可。
      2.在该工程中新增一个automation对象
      选择Delphi编辑器选单中的File/New,在New Item中选择ActiveX 项中的Automation Object选项,系统将显示the Automation Object导向,在物件名称中输入TestObject和选择Multiple Instance选项,按OK键后系统将弹出类型编辑器,其实这时系统已经生成了两个单元(Project1—TLB.pas与unit1.pas),unit1.pas中有如下代码:
      initialization
       TAutoObjectFactory.Create(ComServer, TTestObject,
      Class—TestObject, ciMultiInstance);
      end.
      这是告诉类工厂物件执行的方式与位置。
      3.增加组件中的属性与方法
      在类型编辑器中单击工具栏中的方法按钮增加以下方法:
      OnStartPage(unk:IUnknown)
      OnEndPage.
      Test.
      注意在增加方法OnStartPage时,在Parameters项中增加参数unk,类型为Iunknown,增加三项方法后,在类型编辑器中按Refrash键,Unit1.pas中将会出现三个函数,在每个函数下写如下程序:
      procedure Ttestobject.OnStartPage(unk: IUnknown);
       begin
       m_scriptContext := unk as IScriptingContext;
       end;
      procedure Ttestobject.OnEndPage;
       begin
       m_scriptContext := nil;
      end;
      procedure Ttestobject.test;
      begin
      m—scriptContext.Response.Write(′ActiveX Test For Delphi′);
      end;
      当IIS激活一个ActiveX组件时它会自动寻找组件中是否有OnStartPage与OnEndPage方法,如果存在,服务器将在开启本ASP页时自动执行OnStartPage方法和当本ASP页所有脚本执行完毕后自动执行OnEndPage方法。
      其中mscriptContext 与IScriptingContext是ASP中负责将ASP转换成HTML格式必不可少的变量与类型,因此必须将Unit1.pas文件中的Uses加上ASPTypeLibrary—TLB变成
      unit unit1;
      interface
       uses ComObj, ActiveX, Delphi_TLB, ASPTypeLibrary—TLB, SysUtils;
      ASPTypeLibrary—TLB.Pas文件可以通过类型库引入取得,打开Delphi编辑器中的选单Project/import type library 项,选择the Microsoft Active Server Pages Type library,按OK,Delphi便会自动生成一个ASPTypeLibrary_TLB.Pas。
      4.编译与注册
      选择Delphi编辑器中的选单Project/Compile 项编译文件,然后选择Run/Register ActiveX Server对Project1.dll文件注册。
      5.在ASP文件中运用Project1.dll
      在ASP文件中加入以下文字:
       Set TestASP = Server.CreateObject(Project1.TestObject)
       TestASP.Test
      通过上面的例子,我们可以发现用Delphi编写ASP中的ActiveX服务器组件是十分容易的,加上Delphi丰富的组件与强大的功能,使我们相信运用Delphi编写特殊的ASP服务器组件,如数据库、服务器上各种信息与资源访问等,都会十分方便。