欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手把手教你在.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法

 更新時(shí)間:2013年12月17日 15:50:40   作者:  
這篇文章主要介紹了.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法,有需要的朋友可以參考一下

最近發(fā)現(xiàn)在.NET平臺(tái)下使用Web服務(wù)還是很簡單的。
下面舉個(gè)在.NET平臺(tái)下創(chuàng)建Web服務(wù)的簡單例子。首先用Visul Studio .Net創(chuàng)建一個(gè)C# 項(xiàng)目Asp.Net Web服務(wù)程序,源代碼如下:

復(fù)制代碼 代碼如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace author
{
/// <summary>
/// Service1 的摘要說明。
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 該調(diào)用是 ASP.NET Web 服務(wù)設(shè)計(jì)器所必需的
InitializeComponent();
}

#region 組件設(shè)計(jì)器生成的代碼

//Web 服務(wù)設(shè)計(jì)器所必需的
private IContainer components = null;

/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

// WEB 服務(wù)示例
// HelloWorld() 示例服務(wù)返回字符串 Hello World
// 若要生成,請(qǐng)取消注釋下列行,然后保存并生成項(xiàng)目
// 若要測試此 Web 服務(wù),請(qǐng)按 F5 鍵

// [WebMethod]
// public string HelloWorld()
//{
// return "Hello World!";
//}

}
}

這些代碼都是系統(tǒng)自動(dòng)生成的,從這里可以看到,普通的方法添加了WebMethod屬性后就成了Web方法了。下面給這段代碼添加一個(gè)訪問SQL Server數(shù)據(jù)庫的方法,代碼如下:

復(fù)制代碼 代碼如下:

[WebMethod]
public DataSet DataVisit(string id)
{
string mySelectQuery = "Select au_id, au_fname, au_lname From authors where au_id != '"+id+"'";
string myConn = @"server=localhost; uid=sa; database=pubs";
SqlConnection myConnection = new SqlConnection(myConn);
SqlCommand myCmd = new SqlCommand(mySelectQuery, myConnection);
myConnection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = myCmd;

DataSet myDs = new DataSet();
adapter.Fill(myDs, "author_name");
myConnection.Close();
return myDs;
}


這樣就創(chuàng)建了一個(gè)Web服務(wù)了,在Web應(yīng)用程序里就可以通過添加“Web引用”來使用這個(gè)服務(wù)了。

相關(guān)文章

最新評(píng)論