webservice簡單引用

來源:互聯網
上載者:User

標籤:des   style   blog   class   code   c   

//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.Page12 {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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.