




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
WebService 服务可以返回任何可序列化的对象.本文代码给出返回基本数据类型及实体类结构示例和调用代码示例.WebService代码如下: 1.using System; 2.using System.Collections; 3.using System.ComponentModel; 4.using System.Data; 5.using System.Diagnostics; 6.using System.Web; 7.using System.Web.Services; 8.namespace StudentServer 9. 10. / 11. / 本类实现WebService服务 12. / 提供对各种数据类型的返回例子 13. / 包括: 14. / 基本数据类型(string,ini,bool,long,float等) 15. / 类结构型(class),必须是可序列化的类 16. / DataSet类型 17. / 18. public class Demo : System.Web.Services.WebService 19. 20. public Demo() 21. 22. /CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的 23. InitializeComponent(); 24. 25. #region 组件设计器生成的代码 26. 27. /Web 服务设计器所必需的 28. private IContainer components = null; 29. 30. / 31. / 设计器支持所需的方法 - 不要使用代码编辑器修改 32. / 此方法的内容。 33. / 34. private void InitializeComponent() 35. 36. 37. / 38. / 清理所有正在使用的资源。 39. / 40. protected override void Dispose( bool disposing ) 41. 42. if(disposing & components != null) 43. 44. components.Dispose(); 45. 46. base.Dispose(disposing); 47. 48. 49. #endregion 50. / WEB 服务示例 51. / HelloWorld() 示例服务返回字符串 Hello World 52. / 若要生成,请取消注释下列行,然后保存并生成项目 53. / 若要测试此 Web 服务,请按 F5 键 54. / 55. / 字符串型 56. / 57. / Hello World 58. WebMethod 59. public string HelloWorld() 60. 61. return Hello World; 62. 63. / 64. / 整型 65. / 66. / Int 67. WebMethod 68. public int GetInt() 69. 70. return 1234; 71. 72. 73. / 74. / 布尔型 75. / 76. / Bool 77. WebMethod 78. public bool GetBool() 79. 80. return true; 81. 82. / 83. / 返回实体类 84. / 必须是已序列化的类 85. / lass=alt / 学生类 WebMethod public Student GetStudent() Student stu = new Student(); stu.Name = 张三; stu.Age = 25; stu.Sex = true; return stu; / / 返回DataSet数据类型 / / DataSet WebMethod public DataSet GetDataSet() DataSet ds = new DataSet(); return ds; #region 定义可序列化类 /* * 为避免Framework1.1中返回实体类报错“请求格式无法识别。” * 要在Web.Config文件中添加以下内容: * */ /指示下面的类可序列化 / / 学生基本信息类 / Serializable public class Student / / 构造函数 / public Student() private string name; / / 姓名 / public string Name get return name; set name=value; private bool sex; / / 性别-布尔型变量真为女,假为男 / public bool Sex get return sex; set sex=value; private int age; / / 年龄 / public int Age get return age; set age=value; #endregion 调用WebService服务示例代码如下:1.using System; 2.using System.Drawing; 3.using System.Collections; 文章出处:飞诺网():/course/4_webprogram//netjs/20090826/172369_2.htmusing System.ComponentModel; using System.Windows.Forms; using StudentClient.localhost; namespace StudentClient / / FrmDemo 的摘要说明。 / 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; / / 必需的设计器变量。 / private System.ComponentModel.Container components = null; public FrmDemo() / / Windows 窗体设计器支持所必需的 / InitializeComponent(); / / TODO: 在 InitializeComponent 调用后添加任何构造函数代码 / / / 清理所有正在使用的资源。 / protected override void Dispose( bool disposing ) if( disposing ) if(components != null) components.Dispose(); base.Dispose( disposing ); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要使用代码编辑器修改 / 此方法的内容。 / 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; span 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 / 文章出处:飞诺网():/course/4_webprogram//netjs/20090826/172369_5.html 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.Controls.Add(this.label1); this.Name = FrmDemo; this.Text = FrmDemo; this.ResumeLayout(false); #endregion / / 调用学生信息 / / / 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(); / / DataSet
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年软件工程师初级编程实战题集及解析
- 2025年船厂安全操作规范模拟题集
- 2025年国际金融分析师考试知识点梳理与模拟题集
- 电代煤安全知识培训课件新闻
- 2025年焊接工艺知识笔试模拟题及答案
- 人教版鲸教学课件
- 新解读《GB-T 36771-2018番茄花叶病毒检疫鉴定方法》
- 广西钦州市2024-2025学年高一上学期期末教学质量监测物理试卷(含答案)
- 2025年重庆市中考数学押题试卷(三)(含答案)
- 新解读《GB-T 36140-2018装配式玻纤增强无机材料复合保温墙体技术要求》
- 2025晋中祁县司法协理员招聘笔试备考试题及答案解析
- 农村自建房租房合同范本
- 虚拟化平台日常运维指南与规范
- 2024年梅州市公务员考试行测真题附答案详解(典型题)
- 2025家电购销合同范本
- (2025)纪检监察应知应会试题库与参考答案
- 非煤矿职工职业卫生培训
- 社区居民高血压防治健康讲座
- 2025年湖北省中考化学试题深度解读及答案详解
- Unit 3 Same or DifferentSection A Grammar Focus (3a-3c) 课件-2025-2026学年人教版八年级英语上册
- 管线及设备开启作业安全管理制度与操作流程
评论
0/150
提交评论