VS2013开发asmx接口根据ID查询对象
代码如下
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查询对象的更多相关文章
- VS2013开发asmx接口返回一个自定义XML
1:利用XmlDocument生成一个xml文档返回,代码如下 using System;using System.Collections.Generic;using System.Linq;usin ...
- 根据id查询数据(向前台返回json格式的数据)
/** *@description 根据主键查询Bean */ @RequestMapping(value="/getBean/{getId}") public void getB ...
- 使用mybatis开发Dao的原始方法,实现根据用户id查询一个用户信息 、根据用户名称模糊查询用户信息列表 、添加用户信息等功能
1.需求 将下边的功能实现Dao: 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户信息 2. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类 3.User.xml映 ...
- NX二次开发-UFUN查询对象的类型和子类型UF_OBJ_ask_type_and_subtype
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...
- 早期MyBatis开发与接口式Mybatis开发的简介
早期MyBatis开发与接口式Mybatis开发的简介 一.早期版本的myBatis使用 导jar包 1.配置mybatis.xml的配置文件 1) ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- 15-Django开发REST接口
使用Django开发REST接口 我们以在Django框架中使用的图书以及书中人物案例来写一套支持图书数据增删改查的REST API接口,来理解REST API的开发(前后端均发送JSON格式数据) ...
- Django:前后端分离 djangorestframework开发API接口 serializer序列化认证组件
参考:https://blog.csdn.net/zhangmengran/article/details/84887206 目的: 使用serializer序列化器将QuerySet数据序列化为js ...
- 使用Flask开发简单接口(4)--借助Redis实现token验证
前言 在之前我们已开发了几个接口,并且可以正常使用,那么今天我们将继续完善一下.我们注意到之前的接口,都是不需要进行任何验证就可以使用的,其实我们可以使用 token ,比如设置在修改或删除用户信息的 ...
随机推荐
- SNMP代理软件开发
SNMP代理模块包括6个子模块: SNMP协议主要有五种报文get.get-next.set.get-response,trap.l.get-request操作:从代理进程处提取一个或多个参数值2.g ...
- 手机浏览器跳转APP
背景 对于APP来说,回流分享页是最好的最便宜的也是最病毒式的拉新方式.让新用户去下载APP是重要的.对老用户来说,可以直接调起APP也是提升用户体验和让用户有侵入式体验的重要手段.所以我们一起来看看 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- hibernate对查询的几个知识点
1. query里面的主要方法 list()可以得到查询的结果集 uniqueResult()如果查询的结果只有一条可以用这个 通过这两句可以实现分页,好处是可以跨数据库 query.s ...
- User Agent Strings列表 — Kejun's Blog
曾经,ME 就需要这里的东西了. http://www.useragentstring.com/pages/All/
- [Winform]Cefsharp重写alert与confirm弹窗
摘要 在使用winform内嵌cefsharp浏览本地页面的时候,如果出现alert弹窗,会在标题栏显示页面所在目录.所以想起来重写alert的样式,通过winform的MessageBox进行提示. ...
- 用DELPHI 开发压缩、解压、自解压、加密
引 言:在日常中,我们一定使用过WINZIP.WINRAR这样的出名的压缩软件,就是我们开发软件过程中不免要遇到数据加密.数据压缩的问题!本文中就这一技术问题展开探讨,同时感谢各位网友的技巧,在我每次 ...
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- ssh 多条命令执行
格式:ssh user@ip command 单条命令:ssh user@ip command1 多条命令:ssh user@ip "command1;command2" 不加双引 ...
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...