代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace WebServiceTest
{
/// <summary>
/// Test 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class Test : System.Web.Services.WebService
{ [WebMethod]
public int Test1(int a,int b)
{
return a+b;
}
[WebMethod]
public int Test2(int a, int b)
{
return a - b;
}
[WebMethod]
public XmlDocument Test3(int a, int b)
{
string xmlstr = "<nm values='" + a + "' date='2014-1-2' count='14' page='" + b + "'>"
+ "<domain>张三</domain>"
+ "<domain>张三</domain>"
+ "<domain>张三</domain>"
+ "<domain>张三</domain>" + "</nm>";
//关键
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(xmlstr);
return XmlDoc;
}
[WebMethod]
public XmlDocument GetStudentById(int a)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=Test;uid=sa;pwd=sa";
con.Open();
//创建数据库命令
SqlCommand cmd = con.CreateCommand();
//创建查询语句
cmd.CommandText = "SELECT * FROM stu_test where stu_id="+a;
//从数据库中读取数据流存入reader中
SqlDataReader reader = cmd.ExecuteReader();
//从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
Hashtable ht = new Hashtable();
while (reader.Read())
{
//reader.GetOrdinal("id")是得到ID所在列的index,
//reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回
//reader.GetString(int n)这是将第n列的数据以string 格式返回
int stu_id = reader.GetInt32(reader.GetOrdinal("stu_id"));
string stu_name = reader.GetString(reader.GetOrdinal("stu_name"));
int stu_age = reader.GetInt32(reader.GetOrdinal("stu_age"));
string stu_class= reader.GetString(reader.GetOrdinal("stu_class"));
//格式输出数据 ht.Add("stu_id",stu_id);
ht.Add("stu_name", stu_name);
ht.Add("stu_age", stu_age);
ht.Add("stu_class", stu_class);
} Object stu_id1 = ht["stu_id"].ToString();
Object stu_name1 = ht["stu_name"].ToString();
Object stu_age1 = ht["stu_age"].ToString();
Object stu_class1 = ht["stu_class"].ToString();
string xmlstr = "<student stu_id='" + a + "' date='2014-1-2'>" + "<stu_name>" + stu_name1 + "</stu_name>"
+ "<stu_age>" + stu_age1 + "</stu_age>"
+ "<stu_class>" + stu_class1 + "</stu_class>"
+ "</student>";
//关键
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(xmlstr);
return XmlDoc;
}
}
}

测试

结果如下图

VS2013开发asmx接口根据ID查询对象的更多相关文章

  1. VS2013开发asmx接口返回一个自定义XML

    1:利用XmlDocument生成一个xml文档返回,代码如下 using System;using System.Collections.Generic;using System.Linq;usin ...

  2. 根据id查询数据(向前台返回json格式的数据)

    /** *@description 根据主键查询Bean */ @RequestMapping(value="/getBean/{getId}") public void getB ...

  3. 使用mybatis开发Dao的原始方法,实现根据用户id查询一个用户信息 、根据用户名称模糊查询用户信息列表 、添加用户信息等功能

    1.需求 将下边的功能实现Dao: 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户信息 2. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类 3.User.xml映 ...

  4. NX二次开发-UFUN查询对象的类型和子类型UF_OBJ_ask_type_and_subtype

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  5. 早期MyBatis开发与接口式Mybatis开发的简介

    早期MyBatis开发与接口式Mybatis开发的简介 一.早期版本的myBatis使用 导jar包            1.配置mybatis.xml的配置文件                1) ...

  6. Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...

  7. 15-Django开发REST接口

    使用Django开发REST接口 我们以在Django框架中使用的图书以及书中人物案例来写一套支持图书数据增删改查的REST API接口,来理解REST API的开发(前后端均发送JSON格式数据) ...

  8. Django:前后端分离 djangorestframework开发API接口 serializer序列化认证组件

    参考:https://blog.csdn.net/zhangmengran/article/details/84887206 目的: 使用serializer序列化器将QuerySet数据序列化为js ...

  9. 使用Flask开发简单接口(4)--借助Redis实现token验证

    前言 在之前我们已开发了几个接口,并且可以正常使用,那么今天我们将继续完善一下.我们注意到之前的接口,都是不需要进行任何验证就可以使用的,其实我们可以使用 token ,比如设置在修改或删除用户信息的 ...

随机推荐

  1. allowDefinition='MachineToApplication错误

    配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误信息: 在应用程序级别之外使用注册为 allowDefinition='Mac ...

  2. Visual Studio 2019 preview中体验C# 8.0新语法

    准备工作: Visual Studio 2019 Preview版本中并没有包含所有的C# 8.0的新功能,但目前也有一些可以试用了.在开始之前,需要进行入两项设置: 将Framework设置为.ne ...

  3. Sublime Text 2搭建Go开发环境,代码提示+补全+调试

    本文在已安装Go环境的前提下继续. 1.安装Sublime Text 2 2.安装Package Control. 运行Sublime,按下 Ctrl+`(`在Tab键上边),然后输入以下内容: im ...

  4. Spark RDD的fold和aggregate为什么是两个API?为什么不是一个foldLeft?

    欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/31/spark-fold-aggregate-why-not-foldleft/ 大家都知道Scala标准 ...

  5. 在Delphi中操作快捷方式

    快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...

  6. 排名前16的Java工具类

    原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...

  7. 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)

    在本文档中 本笔记介绍了在Oracle电子商务套件版本12.2中创建自定义应用程序所需的基本步骤.如果您要创建新表单,报告等,则需要自定义应用程序.它们允许您将自定义编写的文件与Oracle电子商务套 ...

  8. TDD:simply mocking a class is not necessarily the best practice

    simply mocking a class is not necessarily the best practice either—it might be better to refactor th ...

  9. JVM调优系列:(五)JVM常用调试参数和工具

    转自:http://blog.csdn.net/opensure/article/details/46715769 JVM常用调试参数: –verbose:gc在虚拟机发生内存回收时在输出设备显示信息 ...

  10. java 日历类Calendar用法

    如何获取昨天?取昨天的日期,本想的截出来日期减一就好了.又一想不对,如果今天是一号怎么办? 现有两个办法 1: Date as = new Date(new Date().getTime()-24*6 ...