Custom functions for common database operations

Source: Internet
Author: User
Tags split table name trim
function | data | database <%
' Some of the common data operations in the development process of ASP, have been sorted into functions, improve the development efficiency

Webdir= "test/" ' path, modified according to the actual situation
function conndb (connname) ' Connect to Database
Set Connname=server.createobject ("Adodb.connection")
Connname.open "Driver={microsoft Access driver (*.mdb)};d bq=" &server.mappath (WebDir & "Inc/mydb.mdb")
' Connname.open ' provider=sqloledb;server=192.168.0.1;uid=sa;pwd=;d atabase=test ' "' uses SQL Server database
End Function

function Close (objectname) ' Closes an object
Objectname.close
Set objectname=nothing
End Function

function Creaters (rsname) ' Creates a DataSet object
Set Rsname=server.createobject ("Adodb.recordset")
End Function

function GetValue (thisvalue)
Getvalue=trim (Request (Thisvalue))
End Function

"" The following function parameter definitions explain
"" Rsname for the definition DataSet object, dataname for the table name, fields for the name of the field to get (comma separated, null for all)
"" Where is the condition, order is sorted
"Eg:call Readdb (RS," tablename1 "," Id,name,sex "," sex= ' Men ' and id>10 "," id desc ")
"" Returns: Recordset object

Sub Readdb (Rsname,dataname,fields,where,order) ' Read records
Dim Source,recrsname,mok
If trim (Fields) = "" Then fields= "*"
Source= "Select" &Fields& "from" &dataname
If Trim (where) <> "then source=source&" where "&where
If trim (order) <> "" Then source=source& ' ORDER by ' &order
Set Recrsname=server.createobject ("ADODB. Recordset ")
Recrsname.open source,conn,3,1
Set rsname = Recrsname
End Sub

Function CountNumber (tablename,where) ' Gets the number of records in the table that meet the criteria
If Len (Trim (where)) >0 Then
Thiswhere= "where" & where
Else
Thiswhere= ""
End If
Set Rs=conn.execute ("SELECT count (*) as Thisnumber from" & TableName & Thiswhere)
Thisnumber=rs ("Thisnumber")
Close RS
Countnumber=thisnumber
End Function

function Insertdb (Dataname,field) ' adds, inserts a new record, requires the submit form individual name to be consistent with the field name in the datasheet.
Dim insertrs,i,datafield,datafieldvalue
Datafield=split (Field, ",")
Set Insertrs=server. CreateObject ("Adodb.recordset")
Insertrs. Open dataname,conn,1,3
Insertrs.addnew
For i=0 to UBound (DataField)
Datafieldvalue=getvalue (DataField (i))
If Len (datafieldvalue) =0 Then
Datafieldvalue= ""
End If
Insertrs (DataField (i)) =datafieldvalue
Next
Insertrs. Update
Close Insertrs
End Function


function Modifydb (dataname,field,where) ' modifies a record that satisfies a condition.
If trim (Field) = "" Then
fields= "*"
Else
Fields=field
End If
Source= "Select" &Fields& "from" &dataname
If Trim (where) <> "then source=source&" where "&where
Datafield=split (Field, ",")
Set Insertrs=server. CreateObject ("Adodb.recordset")
Insertrs. Open source,conn,3,2
While not insertrs.eof
For i=0 to UBound (DataField)
Datafieldvalue=getvalue (DataField (i))
Insertrs (DataField (i)) =datafieldvalue
Next
Insertrs. Update
Insertrs.movenext
Wend
Close Insertrs
End Function

function Deletedb (dataname,where) ' Delete record
Dim delsql
Delsql= "Delete from" &dataname
If Trim (where) <> "then delsql=delsql&" where "&where
Conn.execute Delsql
End Function
%>



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.