ASP.NET教程:调用WebService的源码

2009-08-07 15:44:42  来源:网页教学网 

WebjxCom提示:这里一定要添加WEB 引用菜单步骤如下project->add web reference...,然后输入我们Web Service的路径,这里是http://localhost/WebService1/Service1.asmx,点击添加就OK了。这时你将在类视图中看到localhost命名空间了。

这里一定要添加WEB 引用菜单步骤如下project->add web reference...,然后输入我们Web Service的路径,这里是http://localhost/WebService1/Service1.asmx,点击添加就OK了。这时你将在类视图中看到localhost命名空间了。

WebApplication1
WebForm1.aspx  
代码:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
                Width="568px" Height="256px">
                <P>Panel
                    <asp:Button id="Button3" runat="server" Text="Button3"></asp:Button>
                    <asp:Button id="Button2" runat="server" Text="Button2"></asp:Button>
                    <asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
                    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
                <P>
                    <asp:Label id="Label1" runat="server">Label</asp:Label></P>
                <P>
                    <asp:DataGrid id="DataGrid1" runat="server" Width="568px" Height="120px"></asp:DataGrid></P>
            </asp:Panel>
        </form>
    </body>
</HTML>
====================================================
WebForm1.aspx.cs  
代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Panel Panel1;
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.Button Button3;
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Web.UI.WebControls.Label Label1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            
            // 在此处放置用户代码以初始化页面
        }
        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.Button3.Click += new System.EventHandler(this.Button3_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
        private void Button2_Click(object sender, System.EventArgs e)
        {
            DataSet ds=new DataSet();
            localhost.WebService1 oService=new localhost.WebService1();
            //返回记录集
            ds=oService.GetService1Table();
            if (ds!=null)
            {
                //显示记录集的记录
                DataGrid1.DataSource=ds.Tables["mfTable1"];
                DataGrid1.DataBind();
            }
            else
            {
                this.Response.Write("加载数据错误!");
            }
        }
        //测试GetByUser()
        private void Button1_Click(object sender, System.EventArgs e)
        {
            DataSet ds=new DataSet();
            localhost.WebService1 oService=new localhost.WebService1();
            string mfCommand=TextBox1.Text;
            ds=oService.GetByUser(mfCommand);
            if (ds!=null)
            {
                DataGrid1.DataSource=ds;
                DataGrid1.DataBind();
            }
            else
            {
                Response.Write("错误!有可能是SQL命令有问题!");
            }
        }
        //测试About()
        private void Button3_Click(object sender, System.EventArgs e)
        {
            localhost.WebService1 oService=new localhost.WebService1();
            Label1.Text=oService.About();
        }
    }
}
====================================================
WebService1
Service1.asmx.cs 代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace WebService1
{
    /// <summary>
    /// Service1 的摘要说明。
    /// </summary>
    [WebService(Namespace="http://localhost/WebService1/",Name="WebService1",Description="WebService1")]
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
            InitializeComponent();
        }
        #region 组件设计器生成的代码
        
        //Web 服务设计器所必需的
        private IContainer components = null;
                
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
        }
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if(disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);        
        }
        
        #endregion
        // WEB 服务示例
        // HelloWorld() 示例服务返回字符串 Hello World
        // 若要生成,请取消注释下列行,然后保存并生成项目
        // 若要测试此 Web 服务,请按 F5 键
//        [WebMethod]
//        public string HelloWorld()
//        {
//            return "Hello World";
//        }
//连接字符串常量
        const string mfConn="uid=sa;pwd=mikecat;initial catalog=mf_mikecat;data source=mf";
        [WebMethod]
        public string About()
        {
            return "这是一个C#编写得Web Service演示程序!";
        }
        //返回其中一个WebService1表
        [WebMethod]
        public DataSet GetService1Table()
        {
            DataSet sqlDs=new DataSet();
            DataAccess dataAcc=new DataAccess(mfConn);
            string mfSql="select * from book";
            sqlDs=dataAcc.GetDataset(mfSql,"mfTable1");
            return sqlDs;
        }
        //返回由用户指定得查询
        [WebMethod]
        public DataSet GetByUser(string mfCommandText)
        {
            DataSet sqlDs=new DataSet();
            DataAccess dataAcc=new DataAccess(mfConn);
            sqlDs=dataAcc.GetDataset(mfCommandText);
            return sqlDs;
        }
    }
}
//数据访问类
public class DataAccess
{
//连接字符串变量
    private string connStr="";
    private SqlConnection sqlConn;
    private SqlDataAdapter sqlAda;
//构造函数
    public DataAccess(string mfConnectionString)
    {
        connStr=mfConnectionString;
    }
    public DataSet GetDataset(string mfCommandText)
    {
        DataSet sqlDs;
        try
        {
            sqlConn=new SqlConnection(connStr);
            sqlConn.Open();
            sqlAda=new SqlDataAdapter(mfCommandText,sqlConn);
            sqlDs=new DataSet();
            sqlAda.Fill(sqlDs);
            return sqlDs;
        }
        catch
        {
            return null;
        }
    }
    public DataSet GetDataset(string mfCommandText,string mfTableName)
    {
        DataSet sqlDs;
        try
        {
            sqlConn=new SqlConnection(connStr);
            sqlConn.Open();
            sqlAda=new SqlDataAdapter(mfCommandText,sqlConn);
            sqlDs=new DataSet();
            sqlAda.Fill(sqlDs,mfTableName);
            sqlConn.Close();
            return sqlDs;
        }
        catch
        {
            return null;
        }
    }
}
更多

推荐文章