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
查看全文
相关阅读:
汇编四(习题)
汇编子程序模块化(near&far)
win10关闭防火墙
python中numpy中的shape()的使用
文件的拷贝linux命令
python中的os.path.dirname(__file__)
ubuntu系统下安装及查看opencv版本
用git命令行克隆项目及出现failed解决方案
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '
记录CenterNet代码编译成功运行
原文地址:https://www.cnblogs.com/gwazy/p/152312.html
最新文章
Java 多线程
Linux 命令实时补充
Python Kite 使用教程 轻量级代码提示
汇编 易混淆指令lea offset
算法分析——算法的渐进效率分析
Spring Boot2.2.X中Tomcat配置 错误定制
Spring Boot 异常处理静止trace
Spring Boot 处理异常返回json
Spring Boot Request method DELETE not supported
漫画:三种 “奇葩” 的排序算法代码实现
热门文章
Spring Boot Failed to load resource: the server responded with a status of 404 ()
Spring Boot常用注解和原理整理
Spring Boot中使用Swagger2构建强大的RESTful(最新全,无坑)
Spring Boot 静态资源能加载css 不能加载js
Spring Boot 2.X 国际化
WebMvcConfigurationSupport 避坑指南
GitHub Actions 工作流
C语言与汇编语言混合编程实验
Spring Boot MVC api返回的String无法关联到视图页面
FIRST 集与 FOLLOW 集
Copyright © 2011-2022 走看看