一個改進的ASP產生SQL命令字串類的代碼[已測]

來源:互聯網
上載者:User

複製代碼 代碼如下:<%
class SQLString
'************************************
'變數定義
'************************************
'sTableName ---- 表名
'iSQLType ----SQL語句類型:0-增加,1-更新,2-刪除,3-查詢
'sWhere ---- 條件
'sOrder ---- 排序方式
'sSQL ----值
Private sTableName,iSQLType,sWhere,sOrder,sSQL
'************************************
'類初始化/結束
'************************************
Private Sub Class_Initialize()
sTableName=""
iSQLType=0
sWhere=""
sOrder=""
sSQL=""
End Sub
Private Sub Class_Terminate()
End Sub
'************************************
'屬性
'************************************
'設定表名的屬性
Public Property Let TableName(value)
sTableName=value
End Property
'設定條件
Public Property Let Where(value)
sWhere=value
End Property
'設定排序方式
Public Property Let Order(value)
sOrder=value
End Property
'設定查詢語句的類型
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
'************************************
'函數
'************************************
'增加欄位(欄位名稱,欄位值)
Public Sub AddField(sFieldName,sValue)
select case iSQLType
case 0
sSQL=replace(sSQL,"{&*#}1",sFieldName & ",{&*#}1")
sSQL=replace(sSQL,"{&*#}2","" & sValue & ",{&*#}2")
case 1
sSQL=replace(sSQL,"{&*#}1",sFieldName)
sSQL=replace(sSQL,"{&*#}2","" & sValue & ",{&*#}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<>"" and iSQLType<>0 then
sSQL=sSQL & " where " & sWhere
end if
if sOrder<>"" and iSQLType<>0 then
sSQL=sSQL & " order by " & sOrder
end if
ReturnSQL=sSQL
End Function
'返回SQL語句
Public Function ReturnSQL1()
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<>"" and iSQLType<>0 then
sSQL=sSQL & " where " & sWhere
end if
if sOrder<>"" and iSQLType<>0 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
%>
調用例子:
<%
set a =new SQLString '建立類對象
a.TableName=" message " '設定表名為message
'a.where=" issend =9"
'a.order=" issend desc"
a.SQLType=0 '設定查詢類型為增加記錄
a.AddField " incept", "'2'"
a.AddField " sender ", "'%3%' "
a.AddField " title ", "#"&now&"#"
a.AddField " sender ", "5 "
a.AddField " content ", " 6 "
a.AddField " sendtime ", "7"
a.AddField " flag", 8
a.AddField " issend ", 9
Response.Write a.ReturnSQl
set a=nothing
%>
<%
set a =new SQLString '建立類對象
a.TableName=" message " '設定表名為message
'a.where=" issend =9"
'a.order=" issend desc"
a.SQLType=0 '設定查詢類型為增加記錄
a.AddField " incept", "'2'"
a.AddField " sender ", "'%3%' "
a.AddField " title ", "#"&now&"#"
a.AddField " sender ", "5 "
a.AddField " content ", " 6 "
a.AddField " sendtime ", "7"
a.AddField " flag", 8
a.AddField " issend ", 9
Response.Write a.ReturnSQl
set a=nothing
%>
<%
set a =new SQLString '建立類對象
a.TableName=" message " '設定表名為message
'a.where=" issend =9"
'a.order=" issend desc"
a.SQLType=0 '設定查詢類型為增加記錄
a.AddField " incept", "'2'"
a.AddField " sender ", "'%3%' "
a.AddField " title ", "#"&now&"#"
a.AddField " sender ", "5 "
a.AddField " content ", " 6 "
a.AddField " sendtime ", "7"
a.AddField " flag", 8
a.AddField " issend ", 9
Response.Write a.ReturnSQl
set a=nothing
%>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.