webservice简单引用

//1.创建网站
//2.新建项=>添加web服务
//运行texttweb.asmx可以通过访问http://域名/webservice/texttweb.asmx来验证了
//3.添加服务引用=>发现服务=>确定添加
//4.添加窗体调用webservive对外发布的方法,可以调用显示webservice对外开发的方法了

web服务

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.SqlClient;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Services;
 7 
 8 /// <summary>
 9 /// textweb 的摘要说明
10 /// </summary>
11 [WebService(Namespace = "http://tempuri.org/")]
12 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
14 // [System.Web.Script.Services.ScriptService]
15 public class textweb : System.Web.Services.WebService {
16 
17     public textweb () {
18 
19         //如果使用设计的组件,请取消注释以下行 
20         //InitializeComponent(); 
21     }
22     [WebMethod(Description="这个方法返回一个查询数据库数据结果")]
23     public string HelloWorld()
24     {
25         string name = "";
26         string conString = "data source=.;initial catalog=Texts;user id=sa;pwd=023812;";
27         using (SqlConnection con = new SqlConnection(conString))
28         {
29             con.Open();
30             string sql = "select name from Student where id = 5";
31             SqlCommand com = new SqlCommand(sql, con);
32             name = com.ExecuteScalar().ToString();
33         }
34         return name;
35     }
36     [WebMethod(Description = "这个方法计算加法")]
37     public int Sum(int a,int b) //提供对外的调用
38     {
39         return a + b;
40     }
41     
42 }
View Code

web窗体

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         
14         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
15         <asp:Label ID="Label1" runat="server" Text="+"></asp:Label>
16         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
17         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="=" />
18         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
19         
20     </div>
21     </form>
22 </body>
23 </html>
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Security;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.UI.WebControls.WebParts;
 9 using System.Xml.Linq;
10 
11 public partial class _Default : System.Web.UI.Page
12 {
13     protected void Page_Load(object sender, EventArgs e)
14     {
15 
16     }
17     protected void Button1_Click(object sender, EventArgs e)
18     {
19         textweb web = new textweb();//实例化webservice对象
20         //调用webservice对象提供的方法
21         TextBox3.Text = web.Sum(Convert.ToInt32(TextBox1.Text),       Convert.ToInt32(TextBox2.Text)).ToString();
22     }
23 }    
View Code

 

转载于:https://www.cnblogs.com/LiuZhen/p/3723926.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值