Web Services调用存储过程简单实例

转:http://www.cnblogs.com/jasenkin/archive/2010/03/02/1676634.html

Web Services 主要利用 HTTP 和 SOAP 协议使商业数据在 Web 上传输,SOAP通过 HTTP 调用商业对象执行远程功能调用,Web 用户能够使用 SOAP 和 HTTP通过 Web 调用的方法来调用远程对象.

       web services调用存储过程简单实例:仅供学习,简单易懂!SQLHelper调用。

  页面代码:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnGetResponse" runat="server" Text="get response" OnClick="btnGetResponse_Click" /><br />
        <asp:Label ID="lblShow" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>

cs代码:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public partial class WebServiceSample_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["number"] = 1;
        }
    }
    protected void btnGetResponse_Click(object sender, EventArgs e)
    {
        WebService wss = new WebService();
        lblShow.Text = wss.ResponseInformation()+"  "+ViewState["number"].ToString();
        ViewState["number"] = int.Parse(ViewState["number"].ToString()) + 1;
    }
}

webservice.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>

WebService.cs代码如下

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


using XX.Data;
using System.Data;
using System.Text;

/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
  
    [WebMethod]
    public string ResponseInformation()
    {
        StringBuilder sb = new StringBuilder();
        System.Data.SqlClient.SqlParameter para =new System.Data.SqlClient.SqlParameter("@number",10);
        para.Direction = ParameterDirection.Input;
        DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure,"GetDataSam",para);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (sb.ToString() == "")
                {
                    sb.Append(ds.Tables[0].Rows[0]["title"].ToString());
                }
                else
                {
                    sb.Append("<br/>").Append(ds.Tables[0].Rows[i]["title"].ToString());
                }
            }
        }
        return sb.ToString();
    }


}

存储过程:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->create  procedure GetDataSam(
@number int 
)
as 
begin   
declare @str nvarchar(200);
set   @str='select top '+cast(@number  as  nvarchar)+ 'title  from  zhq_in_content    order by  createdate   desc';
exec(@str);
end

 

posted @ 2017-07-19 17:54  herizai  阅读(717)  评论(0编辑  收藏  举报