A class that integrates the SQL statement

xiaoxiao2021-03-06  51

When we write the ASP database program, we usually use the SQL statement, and when adding data and update data, it usually uses: Insert Into Message (ISSER, SENDTIME, FLAG, ISSEND) Values ​​('"& INCEPT (I) &",' "& MemberName &", '"& Title &",' "& Message &", Now (), 0, 1) When the field is more, and the updated table comparison When you are more, you will be more troublesome, and it is more difficult to find errors. After using this SQL class, you can simplify the modification, and the error is easier. Increase the field name and field value through the class's AddField function, easily insert the field name and field value into the SQL statement, and then return the SQL statement.

Let's take a look at this class code:

<%

Class Sqlstring

'********************************************

'Variable definitions

'********************************************

'stablename ---- Name

'isqltype ---- SQL statement type: 0- Increase, 1-update, 2-delete, 3-query

'Swhere ---- Condition

'sorder ---- Sort by

'ssql ---- value

Private Stablename, Isqltype, Swhere, Sorder, SSQL

'********************************************

'Class initialization / end

'********************************************

Private sub coplass_initialize ()

StableName = ""

Isqltype = 0

Swhere = ""

sorder = ""

SSQL = ""

End Sub

Private sub coplass_terminate ()

End Sub

'********************************************

'Attributes

'********************************************

'Set the properties of the table name

Public property Let Tablename (Value)

Stablename = value

End Property

'Setting the condition

Public property let Where (Value)

Swhere = Value

End Property

'Setting the sorting method

Public property let Order (Value)

sorder = value

End Property

'Set the type of 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 to 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 <> "" "

SSQL = SSQL & "Where" & Swhere

END IF

IF sorder <> "" "

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

%>

Instructions:

Example: INSERT INTO Message (INCEPT, SENDER, TITLE, Content, Sendtime, Flag, ISSEND) VALUES ('"& inscept (i) &",' "& membername &", '"& title &",' "& message &", Now (), 0, 1)

Set a = new sqlstring 'creation class object

a.tablename = "message" 'Settings Name Merssesa.sqlType = 0' Setting the query type is increasing 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

转载请注明原文地址:https://www.9cbs.com/read-81653.html

New Post(0)