asp.net调用WebService

asp.net调用WebService
2008-06-11 10:37
这里一定要添加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>
      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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值