用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧Delphi

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

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:19:11
本文指出了如何在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.

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:25,515.63000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号