|
We usually use SQL statements when writing ASP database programs. When adding and updating data, we usually use the following method: insert into message (incept, sender, title, content, sendtime, flag, issend) values ('"& incept (I) &"', '"& membername &"', '"& Title &"', '"& message &"', now (),) when there are many fields and many updated tables, it will be troublesome to modify them, it is also difficult to find errors. After using this SQL class, you can simplify the modification and check for errors. You can use the addfield function of the class to add the field name and field value. You can easily Insert the field name and field value into an SQL statement and then return the SQL statement.
Let's take a look at the code of this class:
<%
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", "'" & 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
'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 <> "then
Ssql = ssql & "where" & swhere
End if
If sorder <> "" 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
%>
Usage:
Example: insert into message (incept, sender, title, content, sendtime, flag, issend) values ('"& incept (I )&"', '"& membername &"', '"& Title &"', '"& message &"', now (), 0, 1)
Set a = new sqlstring 'create a Class Object
A. tablename = "message" 'set the table name to message.
A. sqltype = 0' set the query type to add records
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 |