用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"></add>
</appSettings>
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
<WebService(Namespace:="http://tempuri.org/")> _
Public Class DownWebService
Inherits System.Web.Services.WebService
#Region " Web 服务设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Web 服务设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加您自己的初始化代码
End Sub
'Web 服务设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Web 服务设计器所必需的
'可以使用 Web 服务设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: 此过程是 Web 服务设计器所必需的
'不要使用代码编辑器修改它。
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#End Region
Public 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"
Public SQL_DOWN_Update As String = "update down set totaldown=totaldown+1 "
Public SQL_DOWN_CLASS As String = "SELECT * from downclass "
'----------------------------------------------------------------
'得到下载的分类
'----------------------------------------------------------------
<WebMethod()> Public Function GetDownClass() As DataSet
Return DataBase.ExecuteReDS(SQL_DOWN_CLASS)
End Function
'----------------------------------------------------------------
'得到下载的所有信息
'----------------------------------------------------------------
<WebMethod()> Public Function GetDownInfo() As DataSet
Return DataBase.ExecuteReDS(SQL_DOWN_SELECT)
End Function
'----------------------------------------------------------------
'从ID得到下载的信息,结果为一条记录
'----------------------------------------------------------------
<WebMethod()> Public Function GetDownFromID(ByVal ID As Integer) As DataSet
Return DataBase.ExecuteReDS(SQL_DOWN_SELECT & " where dbo.Down.id= " & ID)
End Function
'----------------------------------------------------------------
'更新下载次数
'----------------------------------------------------------------
<WebMethod()> Public Function UpdateTotalDown(ByVal ID As Integer) As Boolean
Return DataBase.ExecuteSQL(SQL_DOWN_Update & " where id= " & ID)
End Function
End Class
--------------------------------------------------------------------------
Author : lihonggen0
个人专栏:http://www.csdn.net/develop/author/netauthor/lihonggen0/
如需引用,请指明出处!软件的目的在于应用,本文可自由转载!
--------------------------------------------------------------------------