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
查看全文
相关阅读:
分治fft
DSU on tree
最小矩形覆盖
用gallery展示图片,实现中间图片稍大,两边较小的效果
动态刷新listview中的数据
比较两个文件是否一样
安卓巴士Android开发神贴整理
Failed to register input channel. Check logs for details.
解决scrollview嵌套ImageView时,出现除顶部和底部黑块的问题
获取系统图片,并放入ImageList
原文地址:https://www.cnblogs.com/gwazy/p/152312.html
最新文章
Windows下编译OpenSSL
Windows下编译libcurl
SQL_game_db
bat for /f 用法
Lua基础备忘
Js操作cookies
图片横向移动
代理模式
margintop在chrome中的兼容问题
任务框架quartz在项目中的的运用。。
热门文章
freemarker自定义标签的写法和使用
百度地图在项目里的应用
js&jquery 获得浏览器信息
一个线程同步的例子..
半平面相交
P2774 方格取数问题
模拟退火
最小费用最大流模板
GarsiaWachs算法
二维凸包
Copyright © 2011-2022 走看看