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
查看全文
相关阅读:
结构体初始化所遇到的问题
字符串赋值注意事项
C语言可变参数 "..."
typedef 定义指针数组和数组指针及其使用。
指针函数、指针数组、函数指针、数组指针、函数指针数组。
前端回血day24 flex子项伤的CSS属性
Fiddler使用
一句话“截流”和“防抖”
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.
Linux
原文地址:https://www.cnblogs.com/gwazy/p/152312.html
最新文章
python2
python1
python
Python的基础知识(3)
python
python 装饰器
keepalived配置文件参数
apscheduler模块使用
logging模块
docker daemon
热门文章
docker-compose
yum更新,docker安装
restframework
docker镜像制作Dockerfile
linux apache软件安装
汇编字符串末尾以00H或 0AH和00H结尾
汇编栈帧
lw_oopc(c语言实现面向过程宏文件)解析
C语言实现类
计算结构体内元素的偏移量宏
Copyright © 2011-2022 走看看