用web service 实现连动

ProductsService.asmx

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 WebService
{
    
/// <summary>
    
/// ProductsService 的摘要说明。
    
/// </summary>

    public class ProductsService : System.Web.Services.WebService
    
{
        
public ProductsService()
        
{
            
//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[] GetCategories() 
        
{
            
string strSelect;
            SqlConnection conNorthwind;
            SqlCommand cmdCategories;
            SqlDataReader dtrCategories;
            ArrayList colCategories;
        
            strSelect 
= "SELECT CategoryName FROM Categories";
            conNorthwind 
= new SqlConnection( "server=(local);database=Northwind;uid=sa;pwd=" );
            cmdCategories 
= new SqlCommand( strSelect, conNorthwind );
            conNorthwind.Open();
            dtrCategories 
= cmdCategories.ExecuteReader();
            colCategories 
= new ArrayList();
            
while (dtrCategories.Read())

            colCategories.Add( dtrCategories[
"CategoryName"] );
            conNorthwind.Close();
            
return (string[])colCategories.ToArray( typeof( System.String ) ); 
        }




            [WebMethod] 
            
public string[] GetProducts(string CategoryName) 
            
{
                
string strSelect;
                SqlConnection conNorthwind;
                SqlCommand cmdProducts;
                SqlDataReader dtrProducts;
                ArrayList colProducts;
        
                CategoryName 
= Server.UrlDecode( CategoryName );

                strSelect 
= "SELECT ProductName, UnitPrice " +
                    
"FROM Products, Categories WHERE Products.CategoryID = Categories.CategoryID " +
                    
"AND CategoryName = @CategoryName";

                conNorthwind 
= new SqlConnection(  "server=(local);database=Northwind;uid=sa;pwd=");
                cmdProducts 
= new SqlCommand( strSelect, conNorthwind );
                cmdProducts.Parameters.Add( 
new SqlParameter( "@CategoryName", CategoryName ) );
                conNorthwind.Open();
                dtrProducts 
= cmdProducts.ExecuteReader();
                colProducts 
= new ArrayList();
                
while (dtrProducts.Read()) 
                
{
                    colProducts.Add(
                        dtrProducts[
"ProductName"+ 
                        String.Format( 
" - {0:c}", dtrProducts["UnitPrice"] ) 
                        );
                }

                conNorthwind.Close();
                
return (string[])colProducts.ToArray( typeof( System.String ) ); 
            }

        }

    }




调用页面  


 
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebService.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm2</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">
        
<SCRIPT language="JavaScript">

var intCallID = 0;

function Init()
{
  Service.useService(
"ProductsService.asmx?WSDL","ProductsService");
  intCallID 
= Service.ProductsService.callService( Categories_Result, "GetCategories" );
}



function Categories_Result( result )
{
  
var Categories = new Array();

  Categories 
= result.value;
  
for (var intCounter = 0;intCounter < Categories.length;intCounter++ )
  
{
    
var optOption = new Option( Categories[ intCounter ] );
    frmProducts.dropCategories.options[ frmProducts.dropCategories.options.length ] 
= optOption;
  }

}


function Categories_Change( newCategory )
{
  
var CategoryName = "";
  CategoryName 
= newCategory.options[ newCategory.selectedIndex ].text;
  lblCategory.innerText 
= CategoryName;
  intCallID 
= Service.ProductsService.callService( Products_Result, "GetProducts", CategoryName );
}



function Products_Result( result )
{
  
var Products = new Array();

  Products 
= result.value;
  frmProducts.lstProducts.options.length 
= 0;

  
for (var intCounter = 0;intCounter < Products.length;intCounter++ )
  
{
    
var optOption = new Option( Products[ intCounter ] );
    frmProducts.lstProducts.options[ frmProducts.lstProducts.options.length ] 
= optOption;
  }

 
// frmProducts.lstProducts.style.display = "block";   
}



        
</SCRIPT>
    
</HEAD>
    
<body onload="Init()">
        
<div id="Service" style="BEHAVIOR:url(webservice.htc)"></div>
        
<form id="frmProducts">
            
<select name="dropCategories" onchange="Categories_Change(this)">
                
<option selected>Select Product Category</option>
            
</select>
            
<p>
                
<span id="lblCategory"></span>
                
<br>
                
<select name="lstProducts">
                    
<option selected></option>
                
</select>
        
</form>
        
</P>
    
</body>
</HTML>
 
demo
 
https://files.cnblogs.com/gwazy/WebService.rar

posted on 2005-05-10 12:08  gwazy  阅读(1916)  评论(2编辑  收藏  举报

导航