DataAccess -- WebServices

用WebServices作数据访问层小结:
本小结所有工程:


公共工程: Demo.Common
服务工程: Demo.WebServices
访问工程: Demo.DataAccess
测试工程: Demo.Test

具体实现方法:
一. 新建"类库"工程: Demo.Common
     在该工程中新建类: DemoUtils.vb ,并在该类中添加两个方法: Serialize 和 Deserialize ,具体如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif Public   Class DemoUtils Class DemoUtils
 2InBlock.gif
 3InBlock.gif    'object --> byte()
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Shared Function Serialize()Function Serialize(ByVal obj As ObjectAs Byte()
 5InBlock.gif        Dim objMS As New IO.MemoryStream
 6InBlock.gif        Dim objBF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
 7InBlock.gif        Try
 8InBlock.gif            objBF.Serialize(objMS, obj)
 9InBlock.gif            Return objMS.GetBuffer()
10InBlock.gif        Catch ex As Exception
11InBlock.gif            Return New Byte() {}
12InBlock.gif        Finally
13InBlock.gif            objMS.Dispose()
14InBlock.gif            objMS = Nothing
15InBlock.gif        End Try
16ExpandedSubBlockEnd.gif    End Function

17InBlock.gif
18InBlock.gif    'byte() --> object
19ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Shared Function Deserialize()Function Deserialize(ByVal obj As Byte()) As Object
20InBlock.gif        Dim objMS As New IO.MemoryStream(obj)
21InBlock.gif        Dim objBF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
22InBlock.gif        Try
23InBlock.gif            Return objBF.Deserialize(objMS)
24InBlock.gif        Finally
25InBlock.gif            objMS.Close()
26InBlock.gif        End Try
27ExpandedSubBlockEnd.gif    End Function

28InBlock.gif
29ExpandedBlockEnd.gifEnd Class

二. 新建"ASP.NET Web服务"工程: Demo.WebServices
     1.添加引用: Demo.Common
     2.将services.asmx 改名为 DemoService.asmx
     3.在App_Code/Service.vb 中添加方法: MyWebServicesTest 方法, 具体如下:
 1 None.gif Imports  System.Web
 2 None.gif Imports  System.Web.Services
 3 None.gif Imports  System.Web.Services.Protocols
 4 None.gif Imports  Demo.Common
 5 None.gif
 6 ExpandedBlockStart.gifContractedBlock.gif < WebService( Namespace : Namespace:="http://tempuri.org/")> _
 7InBlock.gif<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 8InBlock.gif<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
 9ExpandedSubBlockStart.gifContractedSubBlock.gifPublic Class ServiceClass Service
10InBlock.gif     Inherits System.Web.Services.WebService
11InBlock.gif
12InBlock.gif    <WebMethod()> _
13ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Function HelloWorld()Function HelloWorld() As String
14InBlock.gif        Return "Hello World"
15ExpandedSubBlockEnd.gif    End Function

16InBlock.gif
17InBlock.gif    <WebMethod()> _
18ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Function MyWebServicesTest()Function MyWebServicesTest() As Byte()
19InBlock.gif        Return Demo.Common.DemoUtils.Serialize("myWebServicesTest_OK")
20ExpandedSubBlockEnd.gif    End Function

21InBlock.gif
22ExpandedSubBlockEnd.gifEnd Class

三. 新建"类库"工程: Demo.DataAccess
    1.添加web引用(>>此解决方法中的web服务>>DemoService), 并命名为: DA_WebServices
    2.新建类: Class_DataAccess.vb, 并在该类中添加方法: GetServices() ,具体如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif Public   Class Class_DataAccess Class Class_DataAccess
 2InBlock.gif    Private Shared m_ws As DA_WebServices.Service
 3InBlock.gif
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Shared Function GetServices()Function GetServices() As DA_WebServices.Service
 5InBlock.gif        If IsNothing(m_ws) Then
 6InBlock.gif            m_ws = New Demo.DataAccess.DA_WebServices.Service
 7InBlock.gif        End If
 8InBlock.gif        Return m_ws
 9ExpandedSubBlockEnd.gif    End Function

10ExpandedBlockEnd.gifEnd Class

11 None.gif

四.新建"windows应用程序"工程: Demo
     1.添加引用: Demo.Common 和 Demo.DataAccess
     2.在Form1.vb 窗体中添加 button 和 label 控件, 在"代码设计器"中写入单击事件,具体如下:
1 ExpandedBlockStart.gif ContractedBlock.gif Public   Class Form1 Class Form1
2InBlock.gif
3ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
4InBlock.gif        Me.Label1.Text = Demo.Common.DemoUtils.Deserialize(Demo.DataAccess.Class_DataAccess.GetServices().MyWebServicesTest())
5ExpandedSubBlockEnd.gif    End Sub

6ExpandedBlockEnd.gifEnd Class

    单击后,出现如下画面:


(将" myWebServicesTest_OK"调出) 测试成功!





转载于:https://www.cnblogs.com/yongwnet/archive/2007/03/10/670629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值