zoukankan
html css js c++ java
用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
查看全文
相关阅读:
基于jQuery仿淘宝产品图片放大镜代码
【家育通】 关于我们
新房装修三大空鼓解决方法 为家居装修做好前奏
MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
MVC5+EF6 入门完整教程十
MVC5+EF6 入门完整教程九
MVC5+EF6 入门完整教程八
MVC5+EF6 入门完整教程七
MVC5+EF6 入门完整教程六
MVC5+EF6 入门完整教程五
原文地址:https://www.cnblogs.com/gwazy/p/152312.html
最新文章
如何在Swift中使用weak修饰Delegate
linux上有多个版本的python,如何进行版本切换
阿里云 ubuntu部署一键v20Ray
如何从Google Drive快速下载大文件
ubuntu 更新python,从3.5更新到3.7
windows批处理bat文件
python3操作ftp
本地Windows上安装 MySQL数据库
mwget的安装与使用
数据采集-数据获取
热门文章
ffmpeg使用
类库(DLL)项目中引用WebService错误:找不到引用协定“Abcds.XXXXSoap”的默认终结点元素。解决办法
js加载更多
js unicode 编码解析关于数据转换为中文的两种方法
关于在Datable在前端如何给返回的数据在方法转换的问题
sql随机抽取数据的方法
C# Server.MapPath的使用方法
IE9版本以下ajax 跨域问题可行解决方法
ajax跨域问题 怎么处理 原因:CORS 头缺少 'Access-Control-Allow-Origin')
关于Cookie和session的一些理解
Copyright © 2011-2022 走看看