Web service

最近在有接触web Service,了解到Web Service 中的方法不能重载,不能有输出参数。想返回多个值的话,方法返回对象。

对象model代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data ;

namespace MyWebService
{
    public class ReturnObject
    {
        private string _Msg;
        public string Msg
        {
            get { return _Msg; }
            set { _Msg = value; }
        }
        private DataTable  _dtRecord;
        public DataTable dtRecord
        {
            get { return _dtRecord; }
            set { _dtRecord = value; }
        }
        private  bool _isSuccess;
        public bool isSuccess
        {
            get { return _isSuccess; }
            set { _isSuccess = value; }
        }
        private int _rowCount;
        public int rowCount
        {
            get { return _rowCount; }
            set { _rowCount = value; }
        }
    }
}

service方法:


         [WebMethod(Description="返回对象")]
        public ReturnObject getUserIfo()
        {

            DataSet ds = new DataSet();
            ReturnObject rbReturn = new ReturnObject();
            try
            {

                string strsql = "Select   USERID,USERNAME,TELEPHONE  from USERS";
                ds = SqlHelper.GetDataSet(strsql);
                rbReturn.dtRecord = ds.Tables[0];
                rbReturn.rowCount = ds.Tables[0].Rows.Count;
                rbReturn.isSuccess = true;
            }
            catch (Exception ex)
            {
                rbReturn.Msg = ex.ToString();
                rbReturn.isSuccess = false;
                throw ex;
               
            }
         
            return rbReturn;
        }


        [WebMethod(Description="返回的对象序列化")]
        public string  getUserIfoSerialize()
        {

            DataSet ds = new DataSet();
            ReturnObject rbReturn = new ReturnObject();
            try
            {

                string strsql = "Select   *  from MyInfo";
                ds = SqlHelper.GetDataSet(strsql);
                rbReturn.dtRecord = ds.Tables[0];
                rbReturn.rowCount = ds.Tables[0].Rows.Count;
                rbReturn.isSuccess = true;
            }
            catch (Exception ex)
            {
                rbReturn.Msg = ex.ToString();
                rbReturn.isSuccess = false;
                throw ex;

            }
            string strResult = JsonConvert.SerializeObject(rbReturn, Formatting.Indented);
            return strResult;
        }


后台调用webService :前台button

   protected void Button1_Click(object sender, EventArgs e)
        {
            LocalService.Service1SoapClient web = new LocalService.Service1SoapClient();
            string str = web.getUserIfoSerialize();

        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值