利用 JSP的思想來做ASP

來源:互聯網
上載者:User
js 這幾天開始接觸JSP裡面一些BEAN的寫法,然後自己想了想,認為其實在ASP裡面也可以採取這一思想來做。雖然不是很純,不徹底,但是能夠把一些邏輯處理分離出來,更適合程式的移植性,提高了開發週期。我自己寫了個類ConnEX包含了一些對資料庫的操作,覺得應該可以包括一大部分的邏輯處理,但是這樣也提高了錯誤幾率,如果你把SQL語句控制的比較好的話,應該是利大於弊的,這裡都是一點點拙見,望大家指正。
程式的功能有了個大體的架構,其實可以自己添加一些功能,比如開始的資料庫連接 ,可以先設定變數然後通過INIT() 來選擇不同類型的資料庫

<%
'*******************************************************************************************
'* 程式:ConnEx.asp
'*
'* 描述:模仿JAVABEAN寫的一個類,專門操作資料庫,提供多種方法操作,但要避免SQL語法錯誤!
'*
'* 作者:田野 Email:Foxty@sina.com
'*
'* 日期:2005.06.0
'*******************************************************************************************


'On Error Resume Next
Class ConnEx
public ConnEx
public DBpath '---------資料庫路徑
public DBtype '---------資料庫類型 1(Access) 2(SqlServer) 3(可擴充)
public ConnMethod '--------串連方式 (DSN,非DSN)
public User
public Pass
Sub Class_initialize
End Sub

Sub Init()
ConnStr = "Driver={Microsoft Access Driver (*.mdb)};dbq="&Server.MapPath("Date.mdb")
Set ConnEx = Server.Createobject("ADODB.CONNECTION")
ConnEx.Open ConnStr
CatchError("Class_Terminate")
End Sub

Sub CatchError( Str )
If Err Then
Err.Clear
Class_Terminate()
Response.Write("捕捉到錯誤,程式結束!在"&Str&"處")
Response.End()
End If
End Sub

'******************************************
'*通過SQL語句來尋找記錄是否存在,容易出錯
'******************************************

Function HasRecordBySql( Sql )
Call CheckSql(Sql,"R")
Dim Rs,HasR
Set Rs = ConnEx.Execute( Sql )
CatchError("HasReordSql")
If Not (Rs.eof Or Rs.bof) Then
HasR = False
Else
HasR = True
End If
Rs.Close
Set Rs = Nothing
HasRecordBySql = HasR
End Function

'***************************************
'*通過ID來尋找記錄是否存在
'***************************************

Function HasRecordById( StrTableName , IntID )
'CheckValue( IntID , 1 )
Dim Rs,HasR
Sql = "Select top 1 * from "&StrTableName&" Where Id = "&IntID
Call CheckSql(Sql,"R")
Set Rs = ConnEx.Execute(Sql)
CatchError("HasRecordByID")
If Not (Rs.eof Or Rs.bof) Then
HasR = False
Else
HasR = True
End If
Rs.close
Set Rs = Nothing
HasRecordById = HasR
End Function

'**********************************************
'*通過SQL語句取得記錄集
'**********************************************
Function GetRsBySql( Sql )
Call CheckSql(Sql,"R")
Dim Rs
Set Rs = Server.CreateObject("Adodb.RecordSet")
Rs.Open Sql,ConnEx,1,1
Set GetRsBySql = Rs
End Function

'*********************************************
'*取得某個欄位的值
'*********************************************
Function GetValueBySql( Sql )
Call CheckSql(Sql,"R")
Dim Rs,ReturnValue
Set Rs = ConnEx.Execute(Sql)
CatchError("GetValueBySql")
If Not( Rs.Eof Or Rs.Bof ) Then
ReturnValue = Rs(0)
Else
ReturnValue = "沒有記錄"
End If
Rs.Close
Set Rs = Nothing
GetValueBySql = ReturnValue
End Function

'==================================================Update,Insert==================================================================

'*********************************************
'*利用SQL修改資料
'*********************************************
Function UpdateBySql( Sql )
Call CheckSql(Sql,"w")
ConnEx.Execute(Sql)
CatchError("UpdateBySql")
UpdateBySql = True
End Function

'********************************************
'*利用SQL語句插入資料
'********************************************
Function InsertBySql(Sql)
Call CheckSql(Sql,"w")
ConnEx.Execute(Sql)
CatchError("InsertBySql")
InsertBySql = True
End Function

'======================================================Delete=============================================================

'*******



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.