您的位置:首页 > 编程语言 > C#

C# 中WebService返回类型(string,int,bool,DataSet,class实体类)示例

2008-12-18 10:55 525 查看
WebService 服务可以返回任何可序列化的对象.本文代码给出返回基本数据类型及实体类结构示例和调用代码示例.
WebService代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;

using System.Web.Services;

namespace StudentServer

{

/// <summary>

/// 本类实现WebService服务

/// 提供对各种数据类型的返回例子

/// 包括:

/// 基本数据类型(string,ini,bool,long,float等)

/// 类结构型(class),必须是可序列化的类

/// DataSet类型

/// </summary>

public class Demo : System.Web.Services.WebService

{

public Demo()

{

//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的

InitializeComponent();

}

#region 组件设计器生成的代码

//Web 服务设计器所必需的

private IContainer components = null;

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if(disposing && components != null)

{

components.Dispose();

}

base.Dispose(disposing);

}

#endregion

// WEB 服务示例

// HelloWorld() 示例服务返回字符串 Hello World

// 若要生成,请取消注释下列行,然后保存并生成项目

// 若要测试此 Web 服务,请按 F5 键

/// <summary>

/// 字符串型

/// </summary>

/// <returns>Hello World</returns>

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

/// <summary>

/// 整型

/// </summary>

/// <returns>Int</returns>

[WebMethod]

public int GetInt()

{

return 1234;

}

/// <summary>

/// 布尔型

/// </summary>

/// <returns>Bool</returns>

[WebMethod]

public bool GetBool()

{

return true;

}

/// <summary>

/// 返回实体类

/// 必须是已序列化的类

/// </summary>

/// <returns>学生类</returns>

[WebMethod]

public Student GetStudent()

{

Student stu = new Student();

stu.Name = "张三";

stu.Age = 25;

stu.Sex = true;

return stu;

}

/// <summary>

/// 返回DataSet数据类型

/// </summary>

/// <returns>DataSet</returns>

[WebMethod]

public DataSet GetDataSet()

{

DataSet ds = new DataSet();

return ds;

}

}

#region 定义可序列化类

/*

* 为避免Framework1.1中返回实体类报错“请求格式无法识别。”

* 要在Web.Config文件中<system.web>添加以下内容:

* <webServices>

<protocols>

<add name="HttpPost" />

<add name="HttpGet" />

</protocols>

</webServices>

*/

//指示下面的类可序列化

/// <summary>

/// 学生基本信息类

/// </summary>

[Serializable]

public class Student

{

/// <summary>

/// 构造函数

/// </summary>

public Student()

{

}

private string name;

/// <summary>

/// 姓名

/// </summary>

public string Name

{

get

{

return name;

}

set

{

name=value;

}

}

private bool sex;

/// <summary>

/// 性别--布尔型变量真为女,假为男

/// </summary>

public bool Sex

{

get

{

return sex;

}

set

{

sex=value;

}

}

private int age;

/// <summary>

/// 年龄

/// </summary>

public int Age

{

get

{

return age;

}

set

{

age=value;

}

}

}

#endregion

}

调用WebService服务示例代码如下:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using StudentClient.localhost;

namespace StudentClient

{

/// <summary>

/// FrmDemo 的摘要说明。

/// </summary>

public class FrmDemo : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.TextBox txtName;

private System.Windows.Forms.Label label5;

private System.Windows.Forms.Button btnStu;

private System.Windows.Forms.Button btnInt;

private System.Windows.Forms.Button btnHello;

private System.Windows.Forms.Button btnDataSet;

private System.Windows.Forms.Button btnBool;

private System.Windows.Forms.TextBox txtSex;

private System.Windows.Forms.TextBox txtAge;

private System.Windows.Forms.TextBox txtOther;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public FrmDemo()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.label4 = new System.Windows.Forms.Label();

this.txtName = new System.Windows.Forms.TextBox();

this.txtSex = new System.Windows.Forms.TextBox();

this.txtAge = new System.Windows.Forms.TextBox();

this.txtOther = new System.Windows.Forms.TextBox();

this.label5 = new System.Windows.Forms.Label();

this.btnStu = new System.Windows.Forms.Button();

this.btnInt = new System.Windows.Forms.Button();

this.btnHello = new System.Windows.Forms.Button();

this.btnDataSet = new System.Windows.Forms.Button();

this.btnBool = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.Location = new System.Drawing.Point(32, 40);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(48, 16);

this.label1.TabIndex = 0;

this.label1.Text = "姓名:";

//

// label2

//

this.label2.Location = new System.Drawing.Point(32, 70);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(48, 16);

this.label2.TabIndex = 1;

this.label2.Text = "性别:";

//

// label3

//

this.label3.Location = new System.Drawing.Point(136, 70);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(48, 16);

this.label3.TabIndex = 2;

this.label3.Text = "年龄:";

//

// label4

//

this.label4.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

this.label4.Location = new System.Drawing.Point(72, 8);

this.label4.Name = "label4";

this.label4.Size = new System.Drawing.Size(136, 24);

this.label4.TabIndex = 3;

this.label4.Text = "学生基本信息";

//

// txtName

//

this.txtName.Location = new System.Drawing.Point(80, 36);

this.txtName.Name = "txtName";

this.txtName.Size = new System.Drawing.Size(144, 21);

this.txtName.TabIndex = 4;

this.txtName.Text = "";

//

// txtSex

//

this.txtSex.Location = new System.Drawing.Point(80, 64);

this.txtSex.Name = "txtSex";

this.txtSex.Size = new System.Drawing.Size(48, 21);

this.txtSex.TabIndex = 5;

this.txtSex.Text = "";

//

// txtAge

//

this.txtAge.Location = new System.Drawing.Point(184, 64);

this.txtAge.Name = "txtAge";

this.txtAge.Size = new System.Drawing.Size(40, 21);

this.txtAge.TabIndex = 6;

this.txtAge.Text = "";

//

// txtOther

//

this.txtOther.Location = new System.Drawing.Point(80, 96);

this.txtOther.Name = "txtOther";

this.txtOther.Size = new System.Drawing.Size(128, 21);

this.txtOther.TabIndex = 7;

this.txtOther.Text = "";

//

// label5

//

this.label5.Location = new System.Drawing.Point(32, 100);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(48, 16);

this.label5.TabIndex = 8;

this.label5.Text = "其它:";

//

// btnStu

//

this.btnStu.Location = new System.Drawing.Point(8, 128);

this.btnStu.Name = "btnStu";

this.btnStu.Size = new System.Drawing.Size(88, 24);

this.btnStu.TabIndex = 9;

this.btnStu.Text = "调用Student";

this.btnStu.Click += new System.EventHandler(this.btnStu_Click);

//

// btnInt

//

this.btnInt.Location = new System.Drawing.Point(200, 128);

this.btnInt.Name = "btnInt";

this.btnInt.Size = new System.Drawing.Size(32, 24);

this.btnInt.TabIndex = 10;

this.btnInt.Text = "Int";

this.btnInt.Click += new System.EventHandler(this.btnInt_Click);

//

// btnHello

//

this.btnHello.Location = new System.Drawing.Point(152, 128);

this.btnHello.Name = "btnHello";

this.btnHello.Size = new System.Drawing.Size(48, 24);

this.btnHello.TabIndex = 11;

this.btnHello.Text = "Hello";

this.btnHello.Click += new System.EventHandler(this.btnHello_Click);

//

// btnDataSet

//

this.btnDataSet.Location = new System.Drawing.Point(96, 128);

this.btnDataSet.Name = "btnDataSet";

this.btnDataSet.Size = new System.Drawing.Size(56, 24);

this.btnDataSet.TabIndex = 12;

this.btnDataSet.Text = "DataSet";

this.btnDataSet.Click += new System.EventHandler(this.btnDataSet_Click);

//

// btnBool

//

this.btnBool.Location = new System.Drawing.Point(232, 128);

this.btnBool.Name = "btnBool";

this.btnBool.Size = new System.Drawing.Size(40, 24);

this.btnBool.TabIndex = 13;

this.btnBool.Text = "Bool";

this.btnBool.Click += new System.EventHandler(this.btnBool_Click);

//

// FrmDemo

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(280, 158);

this.Controls.Add(this.btnBool);

this.Controls.Add(this.btnDataSet);

this.Controls.Add(this.btnHello);

this.Controls.Add(this.btnInt);

this.Controls.Add(this.btnStu);

this.Controls.Add(this.label5);

this.Controls.Add(this.txtOther);

this.Controls.Add(this.txtAge);

this.Controls.Add(this.txtSex);

this.Controls.Add(this.txtName);

this.Controls.Add(this.label4);

this.Controls.Add(this.label3);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Name = "FrmDemo";

this.Text = "FrmDemo";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 调用学生信息

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnStu_Click(object sender, System.EventArgs e)

{

//实例化服务类

Demo dm = new Demo();

//调用返回实体类服务方法

Student stu = dm.GetStudent();

txtName.Text = stu.Name;

txtSex.Text = (stu.Sex==false?"女":"男");

txtAge.Text = stu.Age.ToString();

}

/// <summary>

/// DataSet数据

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnDataSet_Click(object sender, System.EventArgs e)

{

//实例化服务类

Demo dm = new Demo();

txtOther.Text = dm.GetDataSet().Tables.Count.ToString();

}

/// <summary>

/// 返回字符串

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnHello_Click(object sender, System.EventArgs e)

{

//实例化服务类

Demo dm = new Demo();

txtOther.Text = dm.HelloWorld();

}

/// <summary>

/// 返回整型

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnInt_Click(object sender, System.EventArgs e)

{

//实例化服务类

Demo dm = new Demo();

txtOther.Text = dm.GetInt().ToString();

}

/// <summary>

/// 返回布尔型

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnBool_Click(object sender, System.EventArgs e)

{

//实例化服务类

Demo dm = new Demo();

txtOther.Text = dm.GetBool().ToString();

}

}

}

窗体界面如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐