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

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

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

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

復制代碼 代碼如下:

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 服務設計器所必需的
InitializeComponent();
}

#region 組件設計器生成的代碼

//Web 服務設計器所必需的
private IContainer components = null;

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(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 服務示例
// HelloWorld() 示例服務返回字符串 Hello World
// 若要生成,請取消注釋下列行,然后保存并生成項目
// 若要測試此 Web 服務,請按 F5 鍵

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

}
}

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

復制代碼 代碼如下:

[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)建了一個Web服務了,在Web應用程序里就可以通過添加“Web引用”來使用這個服務了。

相關文章

最新評論