這裡一定要添加WEB 引用菜單步驟如下project->add web reference...,然後輸入我們Web Service的路徑,這裡是http://localhost/WebService1/Service1.asmx,點擊添加就OK了。這時你將在類視圖中看到localhost命名空間了。
<%@ 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>
====================================================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();
}
}
}
====================================================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>
[WebService(Namespace="http://localhost/WebService1/",Name="WebService1",Description="WebService1")]
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;initial catalog=mf_mikecat;data source=mf";
[WebMethod]
public string About()
{
return "這是一個C#編寫得Web Service演示程序!";
}
//返回其中一個WebService1表
[WebMethod]
public DataSet GetService1Table()
{
DataSet sqlDs=new DataSet();
DataAccess dataAcc=new DataAccess(mfConn);
string mfSql="select * from book";
sqlDs=dataAcc.GetDataset(mfSql,"mfTable1");
return sqlDs;
}
//返回由用戶指定得查詢
[WebMethod]
public DataSet GetByUser(string mfCommandText)
{
DataSet sqlDs=new DataSet();
DataAccess dataAcc=new DataAccess(mfConn);
sqlDs=dataAcc.GetDataset(mfCommandText);
return sqlDs;
}
}
}
//數據訪問類
public class DataAccess
{
//連接字符串變量
private string connStr="";
private SqlConnection sqlConn;
private SqlDataAdapter sqlAda;
//構造函數
public DataAccess(string mfConnectionString)
{
connStr=mfConnectionString;
}
public DataSet GetDataset(string mfCommandText)
{
DataSet sqlDs;
try
{
sqlConn=new SqlConnection(connStr);
sqlConn.Open();
sqlAda=new SqlDataAdapter(mfCommandText,sqlConn);
sqlDs=new DataSet();
sqlAda.Fill(sqlDs);
return sqlDs;
}
catch
{
return null;
}
}
public DataSet GetDataset(string mfCommandText,string mfTableName)
{
DataSet sqlDs;
try
{
sqlConn=new SqlConnection(connStr);
sqlConn.Open();
sqlAda=new SqlDataAdapter(mfCommandText,sqlConn);
sqlDs=new DataSet();
sqlAda.Fill(sqlDs,mfTableName);
sqlConn.Close();
return sqlDs;
}
catch
{
return null;
}
}
}