明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

ASP整合的一個SQL語句類

[摘要]我們在寫ASP數(shù)據(jù)庫程序的時候,通常都會用到SQL語句,而在增加數(shù)據(jù)和更新數(shù)據(jù)的時候,通常會使用一下方式:insert into message (incept,sender,title,conte...

我們在寫ASP數(shù)據(jù)庫程序的時候,通常都會用到SQL語句,而在增加數(shù)據(jù)和更新數(shù)據(jù)的時候,通常會使用一下方式:insert into message (incept,sender,title,content,sendtime,flag,issend) values ('"&incept(i)&"','"&membername&"','"&title&"','"&message&"',Now(),0,1) 當(dāng)字段比較多的時候,而且更新的表比較多的時候,修改起來會比較麻煩,而且查找錯誤也比較困難。使用這個SQL類后可以簡化修改,而且查錯也比較容易。通過類的AddField函數(shù)增加字段名和字段值,可輕松的將字段名和字段值插入SQL語句,然后返回該SQL語句。

  下面讓我們看看這個類的代碼:

<%
class SQLString
'************************************
'變量定義
'************************************
'sTableName ---- 表名
'iSQLType ----SQL語句類型:0-增加,1-更新,2-刪除,3-查詢
'sWhere ---- 條件
'sOrder ---- 排序方式
'sSQL ----值

Private sTableName,iSQLType,sWhere,sOrder,sSQL

'************************************
'類初始化/結(jié)束
'************************************

Private Sub Class_Initialize()
 sTableName=""
 iSQLType=0
 sWhere=""
 sOrder=""
 sSQL=""
End Sub

Private Sub Class_Terminate()

End Sub

'************************************
'屬性
'************************************
‘設(shè)置表名的屬性

Public Property Let TableName(value)

 sTableName=value

End Property

‘設(shè)置條件

Public Property Let Where(value)

 sWhere=value

End Property

‘設(shè)置排序方式

Public Property Let Order(value)

 sOrder=value

End Property

‘設(shè)置查詢語句的類型

Public property Let SQLType(value)

 iSQLType=value
 select case iSQLType
 case 0
  sSQL="insert into #0 (#1) values (#2)"
 case 1
  sSQL="update #0 set #1=#2"
 case 2
  sSQL="delete from #0 "
 case 3
  sSQL="select #1 from #0 "
 end select
End Property

'************************************
'函數(shù)
'************************************
'增加字段(字段名稱,字段值)

Public Sub AddField(sFieldName,sValue)
 select case iSQLType
 case 0
  sSQL=replace(sSQL,"#1",sFieldName & ",#1")
  sSQL=replace(sSQL,"#2","'" & sFieldName & "',#2")
 case 1
  sSQL=replace(sSQL,"#1",sFieldName)
  sSQL=replace(sSQL,"#2","'" & sFieldName & "',#1=#2")
 case 3
  sSQL=replace(sSQL,"#1",sFieldName & ",#1")
 End Select
End Sub

 '返回SQL語句
Public Function ReturnSQL()
 sSQL=replace(sSQL,"#0",sTableName)
 select case iSQLType
 case 0
  sSQL=replace(sSQL,",#1","")
  sSQL=replace(sSQL,",#2","")
 case 1
  sSQL=replace(sSQL,",#1=#2","")
 case 3
  sSQL=replace(sSQL,",#1","")
 end Select
 if sWhere<>"" then
  sSQL=sSQL & " where " & sWhere
 end if
 if sOrder<>"" then
  sSQL=sSQL & " order by " & sOrder
 end if
 ReturnSQL=sSQL
End Function

'清空語句

Public Sub Clear()
 sTableName=""
 iSQLType=0
 sWhere=""
 sOrder=""
 sSQL=""

End Sub

end class

%>
 

  使用方法:

  例句:insert into message (incept,sender,title,content,sendtime,flag,issend) values ('"&incept(i)&"','"&membername&"','"&title&"','"&message&"',Now(),0,1)

set a =new SQLString ‘創(chuàng)建類對象
a.TableName=" message " ‘設(shè)置表名為message
a.SQLType=0 ‘設(shè)置查詢類型為增加記錄
a.AddField " incept", incept(i)
a.AddField " sender ", membername
a.AddField " title ", membername
a.AddField " sender ", title
a.AddField " content ", message
a.AddField " sendtime ", sendtime()
a.AddField " flag", 0
a.AddField " issend ", 1
Response.Write a.ReturnSQl
set a=nothing