用WebServices作数据访问层小结:
本小结所有工程:
公共工程: Demo.Common
服务工程: Demo.WebServices
访问工程: Demo.DataAccess
测试工程: Demo.Test
具体实现方法:
一. 新建"类库"工程: Demo.Common
在该工程中新建类: DemoUtils.vb ,并在该类中添加两个方法: Serialize 和 Deserialize ,具体如下:
1
Public
Class DemoUtils
Class DemoUtils
2![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
3
'object --> byte()
4![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Shared Function Serialize()Function Serialize(ByVal obj As Object) As Byte()
5
Dim objMS As New IO.MemoryStream
6
Dim objBF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
7
Try
8
objBF.Serialize(objMS, obj)
9
Return objMS.GetBuffer()
10
Catch ex As Exception
11
Return New Byte() {}
12
Finally
13
objMS.Dispose()
14
objMS = Nothing
15
End Try
16
End Function
17![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
18
'byte() --> object
19![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Shared Function Deserialize()Function Deserialize(ByVal obj As Byte()) As Object
20
Dim objMS As New IO.MemoryStream(obj)
21
Dim objBF As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
22
Try
23
Return objBF.Deserialize(objMS)
24
Finally
25
objMS.Close()
26
End Try
27
End Function
28![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
29
End Class
二. 新建"ASP.NET Web服务"工程: Demo.WebServices
1.添加引用: Demo.Common
2.将services.asmx 改名为 DemoService.asmx
3.在App_Code/Service.vb 中添加方法: MyWebServicesTest 方法, 具体如下:
1
Imports
System.Web
2
Imports
System.Web.Services
3
Imports
System.Web.Services.Protocols
4
Imports
Demo.Common
5
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
6
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
<
WebService(
Namespace :
Namespace:="http://tempuri.org/")> _
7
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
8
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
9![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Class ServiceClass Service
10
Inherits System.Web.Services.WebService
11![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
12
<WebMethod()> _
13![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Function HelloWorld()Function HelloWorld() As String
14
Return "Hello World"
15
End Function
16![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
17
<WebMethod()> _
18![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Function MyWebServicesTest()Function MyWebServicesTest() As Byte()
19
Return Demo.Common.DemoUtils.Serialize("myWebServicesTest_OK")
20
End Function
21![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
22
End Class
三. 新建"类库"工程: Demo.DataAccess
1.添加web引用(>>此解决方法中的web服务>>DemoService), 并命名为: DA_WebServices
2.新建类: Class_DataAccess.vb, 并在该类中添加方法: GetServices() ,具体如下:
1
Public
Class Class_DataAccess
Class Class_DataAccess
2
Private Shared m_ws As DA_WebServices.Service
3![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
4![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Public Shared Function GetServices()Function GetServices() As DA_WebServices.Service
5
If IsNothing(m_ws) Then
6
m_ws = New Demo.DataAccess.DA_WebServices.Service
7
End If
8
Return m_ws
9
End Function
10
End Class
11
四.新建"windows应用程序"工程: Demo
1.添加引用: Demo.Common 和 Demo.DataAccess
2.在Form1.vb 窗体中添加 button 和 label 控件, 在"代码设计器"中写入单击事件,具体如下:
1
Public
Class Form1
Class Form1
2![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
3![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
4
Me.Label1.Text = Demo.Common.DemoUtils.Deserialize(Demo.DataAccess.Class_DataAccess.GetServices().MyWebServicesTest())
5
End Sub
6
End Class
单击后,出现如下画面:
(将"
myWebServicesTest_OK"调出) 测试成功!