An ASP-consolidated SQL statement class

Source: Internet
Author: User
statement when we write ASP database programs, we usually use SQL statements, and when we add data and update the data, we usually use the way: INSERT into message (Incept,sender,title,content,sendtime, Flag,issend) VALUES (' "&incept (i) &", ' &membername& "', '" &title& ", '" &message& "', Now (), 0, 1) When the field is more, and the updated table is more, it will be more cumbersome to modify, and find errors are more difficult. Using this SQL class simplifies the modification and makes it easy to find errors. By adding field names and field values through the AddField function of the class, you can easily insert field names and field values into the SQL statement, and then return the SQL statement.

Let's take a look at the code for this class:

<%
Class SqlString
'************************************
' Variable definition
'************************************
' sTableName----table name
' isqltype----SQL statement type: 0-Add, 1-update, 2-delete, 3-query
' Swhere----conditions
' Sorder----Sorting method
' sSQL----value

Private Stablename,isqltype,swhere,sorder,ssql

'************************************
' Class initialization/end
'************************************

Private Sub Class_Initialize ()
Stablename= ""
Isqltype=0
Swhere= ""
Sorder= ""
Ssql= ""
End Sub

Private Sub Class_Terminate ()

End Sub

'************************************
' Property
'************************************
' Set the properties of the table name

Public Property Let TableName (value)

Stablename=value

End Property

' Set condition

Public Property Let Where (value)

Swhere=value

End Property

' Set sort style

Public Property Let order (value)

Sorder=value

End Property

' Set the type of the query statement

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 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

' Empty statement

Public Sub Clear ()
Stablename= ""
Isqltype=0
Swhere= ""
Sorder= ""
Ssql= ""

End Sub

End Class

%>


How to use:

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 class object
A.tablename= ' message ' Sets the table name as message
A.sqltype=0 ' Set query type to add record
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



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.