用 ASP 、 NET 开发下载系统 ( 二 )
** 中间层 ** ** Web Service **
总体概括
中间层主要是数据库与前台界面的交互桥梁, DownWebService
主要提供以下方法;
'----------------------------------------------------------------
' 得到下载的分类 GetDownClass
' 得到下载的所有信息 GetDownInfo
' 从 ID 得到下载的信息,结果为一条记录 GetDownFromID
' 更新下载次数 UpdateTotalDown
'----------------------------------------------------------------
我们建立了一个通用的类 DataBase.vb 来进行通用的数据库操作,这些代码是我们通常都要用到的,所以我们封装到了一起。
数据库连接字符串存储在 Web.config 中:
< appSettings >
< add key ="connString" value ="Password=sa;User ID=sa;Initial Catalog=Northwind;Data Source=pmserver;Packet Size=4096">

DataBase.vb 类
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Public Class DataBase
'----------------------------------------------------------------
' Sub ExecuteReDS:
' Used for query operations
' Return: result in a dataset
' Parameters:
' [in] cmdText: Sql or Sp name.
'----------------------------------------------------------------
Public Shared Function ExecuteReDS( ByVal cmdText As String ) As DataSet
Dim connString As String = ConfigurationSettings.AppSettings("connString")
Dim conn As New SqlConnection(connString)
Dim adp As SqlDataAdapter = New SqlDataAdapter(cmdText, conn)
Dim ds As DataSet = New DataSet("tb")
Try
adp.Fill(ds)
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
Return ds
End Function
'----------------------------------------------------------------
' Sub ExecuteReDV:
' Return: result in a DataView
' Parameters:
' [in] cmdText: Sql or Sp name.
'----------------------------------------------------------------
Public Shared Function ExecuteReDV( ByVal cmdText As String ) As DataView
Dim connString As String = ConfigurationSettings.AppSettings("connString")
Dim conn As New SqlConnection(connString)
Dim adp As SqlDataAdapter = New SqlDataAdapter(cmdText, conn)
Dim dv As DataView
Dim dt As DataTable
Try
adp.Fill(dt)
dv = dt.DefaultView
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
Return dv
End Function
'----------------------------------------------------------------
' Sub ExecuteSQL:
' Execute SQL
' Return: True or False
' Parameters:
' [in] cmdText: Sql or Sp name.
'----------------------------------------------------------------
Public Shared Function ExecuteSQL( ByVal cmdText As String ) As Boolean
Dim connString As String = ConfigurationSettings.AppSettings("connString")
Dim conn As New SqlConnection(connString)
conn.Open()
Dim cmd As New SqlCommand(cmdText, conn)
Try
cmd.ExecuteScalar()
Catch e As Exception
Return False
Throw e
Finally
conn.Close()
End Try
Return True
End Function
End Class
WebService
** DownWebService.asmx.vb **
Imports System.Web.Services
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
1<webservice( )="" :="http://tempuri.org/" namespace=""> _
2
3Public Class DownWebService
4
5Inherits System.Web.Services.WebService
6
7#Region " Web 服务设计器生成的代码 "
8
9Public Sub New()
10
11MyBase.New()
12
13' 该调用是 Web 服务设计器所必需的。
14
15InitializeComponent()
16
17' 在 InitializeComponent() 调用之后添加您自己的初始化代码
18
19End Sub
20
21'Web 服务设计器所必需的
22
23Private components As System.ComponentModel.IContainer
24
25' 注意:以下过程是 Web 服务设计器所必需的
26
27' 可以使用 Web 服务设计器修改此过程。
28
29' 不要使用代码编辑器修改它。
30
31<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
32
33components = New System.ComponentModel.Container()
34
35End Sub
36
37Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
38
39'CODEGEN: 此过程是 Web 服务设计器所必需的
40
41' 不要使用代码编辑器修改它。
42
43If disposing Then
44
45If Not (components Is Nothing) Then
46
47components.Dispose()
48
49End If
50
51End If
52
53MyBase.Dispose(disposing)
54
55End Sub
56
57#End Region
58
59Public SQL_DOWN_SELECT As String = "SELECT Down.ID,Down.title,Down.description,Down.filename,Down.uploadtime, Down.totaldown, DownClass.classname FROM dbo.Down INNER JOIN dbo.DownClass ON dbo.Down.classID = dbo.DownClass.id"
60
61Public SQL_DOWN_Update As String = "update down set totaldown=totaldown+1 "
62
63Public SQL_DOWN_CLASS As String = "SELECT * from downclass "
64
65'----------------------------------------------------------------
66
67' 得到下载的分类
68
69'----------------------------------------------------------------
70
71<webmethod()> Public Function GetDownClass() As DataSet
72
73Return DataBase.ExecuteReDS(SQL_DOWN_CLASS)
74
75End Function
76
77'----------------------------------------------------------</webmethod()></system.diagnostics.debuggerstepthrough()></webservice(>