如何用delphi2005的WebService返回各种类型数据

一、 建立WebService文件,
二、 引入.Net下的Unit,并改名为ClassFactory,
三、 在WebService中的Uses中加入ClassFactory。
四、 注意WebService中的每一个方法都要有[WebMethod]作引导。
具体程序如下:

==========================以下为WebService的单元文件==========================
unit WebService1;

interface

uses
 System.Collections, System.ComponentModel,
 System.Data, System.Diagnostics, System.Web,
 System.Web.Services, System.Data.Odbc, Borland.Data.Provider,
 ClassFactory;

type
 /// <summary>
 /// Summary description for WebService1.
 /// </summary>
 TWebService1 = class(System.Web.Services.WebService)
 {$REGION 'Designer Managed Code'}
 strict private
  /// <summary>
  /// Required designer variable.
  /// </summary>
  procedure InitializeComponent;
 {$ENDREGION}
 strict protected
  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  procedure Dispose(disposing: boolean); override;
 private
  { Private Declarations }
 public
  constructor Create;
  // Sample Web Service Method
  [WebMethod]
  function HelloWorld: string;
  [WebMethod]
  function YourName(NameNo:Integer): string;
  [WebMethod]
  function getEmp(NameNo:Integer): TEmpInfo;
  [WebMethod]
  function getDoubleArray(DoubleNo:Double): TArrayOfDouble;
  [WebMethod]
  function getIntegerArray(IntegerNo:Integer): TArrayOfInt;



 end;

implementation

{$REGION 'Designer Managed Code'}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWebService1.InitializeComponent;
begin

{$ENDREGION}

constructor TWebService1.Create;
begin
 inherited;
 //
 // Required for Designer support
 //
 InitializeComponent;
 //
 // TODO: Add any constructor code after InitializeComponent call
 //
end;

/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure TWebService1.Dispose(disposing: boolean);
begin
 if disposing and (components <> nil) then
  components.Dispose;
 inherited Dispose(disposing);
end;

// Sample Web Service Method
// The following method is provided to allow for testing a new web service.
function TWebService1.HelloWorld: string;
begin
 Result := 'Hello World';
end;

function TWebService1.YourName(NameNo: Integer): string;
begin
 if NameNo<>0 then Result:='YourName is Bird'
 else Result:='YourName Is null';
end;

function TWebService1.getEmp(NameNo: Integer): TEmpInfo;
begin
 Result:=TEmpInfo.create;
 Result.Name :='Apple';
 Result.Age  :=13+NameNo;
end;

function TWebService1.getDoubleArray(DoubleNo: Double): TArrayOfDouble;
var
 I:Integer;
begin
 SetLength(Result,6);
 For I:=0 to 5 do
  begin
   Result[I]:=DoubleNo+I;
  end;
end;

function TWebService1.getIntegerArray(IntegerNo: Integer): TArrayOfInt;
var
 I:Integer;
begin
 SetLength(Result,6);
 For I:=0 to 5 do
  begin
   Result[I]:=IntegerNo+I;
  end;
end;

end.



========================以下为ClassFactory的单元文件========================
unit ClassFactory;

interface

type
TEmpInfo = Class
 Private
  FName  :String;
  FAge   :Integer;
 Published
  Property Name  :String  Read FName  Write FName;
  Property Age  :Integer Read FAge  Write FAge;
end;

TArrayOfInt   = array of Integer;

TArrayOfDouble  = array of Double;

implementation

end.

转载于:https://www.cnblogs.com/myamanda/articles/1522726.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值