An Improved ASP code for generating SQL command string classes [tested]

Source: Internet
Author: User

Copy codeThe Code is as follows: <%
Class SQLString
'************************************
'Variable definition
'************************************
'Tablename ---- table name
'Isqltype ---- SQL statement type: 0-add, 1-Update, 2-delete, 3-Query
'Swhere ---- Condition
'Sorder ---- sorting method
'Ssql ---- Value
Private sTableName, iSQLType, sWhere, sOrder, sSQL
'************************************
'Class initialization/termination
'************************************
Private Sub Class_Initialize ()
STableName = ""
ISQLType = 0
SWhere = ""
SOrder = ""
SSQL = ""
End Sub
Private Sub Class_Terminate ()
End Sub
'************************************
'Attribute
'************************************
'Set the table name attributes
Public Property Let TableName (value)
STableName = value
End Property
'Set conditions
Public Property Let Where (value)
SWhere = value
End Property
'Set the sorting method
Public Property Let Order (value)
SOrder = value
End Property
'Set the query statement type
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
'************************************
'Function
'************************************
'Add a field (field name, field value)
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
'Modified function to return the string value
'Return SQL statement
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
'Return SQL statement
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
'Clear statement
Public Sub Clear ()
STableName = ""
ISQLType = 0
SWhere = ""
SOrder = ""
SSQL = ""
End Sub
End class
%>
Call example:
<%
Set a = new SQLString 'create a Class Object
A. TableName = "message" 'set the table name to message.
'A. where = "issend = 9"
'A. order = "issend desc"
A. SQLType = 0' set the query type to add records
A. AddField "incept", "'2 '"
A. AddField "sender", "'% 100 '"
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 'create a Class Object
A. TableName = "message" 'set the table name to message.
'A. where = "issend = 9"
'A. order = "issend desc"
A. SQLType = 0' set the query type to add records
A. AddField "incept", "'2 '"
A. AddField "sender", "'% 100 '"
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 'create a Class Object
A. TableName = "message" 'set the table name to message.
'A. where = "issend = 9"
'A. order = "issend desc"
A. SQLType = 0' set the query type to add records
A. AddField "incept", "'2 '"
A. AddField "sender", "'% 100 '"
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
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.