Publish an asp adodb code

Source: Internet
Author: User

I have used this for many websites and I think it is quite useful. So I will share it with you. Three database connections are supported: MSSQL2000, MSSQL2005, and ACCESS
Three methods:
Select_table (SQL)
Returns TRUE or FALSE for table queries.
FALSE is returned when an SQL statement error occurs or a null record occurs. Otherwise, TRUE is returned.
Update_table (SQL)
Table update, including update and delete
If execution is successful, TRUE is returned. Otherwise, FALSE is returned. If updated is executed, the number of records is affected.
Insert_table (SQL, table, id_column)
Table is the table name, id_column is the table's automatic number, and the auto-increment field.
If the operation is successful, TRUE is returned. Otherwise, FALSE is returned. After TABLE and ID_column are specified, the auto-increment ID generated by the last added record is returned.

Select_table () related method Select_page (page, psize)
Page processing, page is the current page, and psize is the number of records per page.
During all operations, the system automatically checks whether the database link and RS are enabled. After the operation is executed, the database link is automatically closed.

Example:
Set db = new adodb_class
If db. select_table ("select * from news order by id desc") then
Page = request ("page ")
Select_page (page, 20) '20 entries per page
For I = 1 to 20
Response. write db. rs ("title") 'class built-in rs, immutable
Db. rs. movenext
If db. rs. eof then exit
Next
End if
Db. rsPage = total number of pages, db. nowPage = Total number of records of db. rsCounts on the current page after processing.

If db. update_table ("delete from news where ispass = 1") then' update is the same
Response. write "delete" & db. updated & "row"
End if

Call db. insert_table ("insert into news (title, content) values ('" & title & "', '" & content & "')", "news", "id ")
Response. write "Last added ID as" & db. Insertd

At the end of the page, you can output db. readCounts as the number of queries to the database.
--------------------------------------------
This type of benefit is that you do not have to worry about forgetting to close the database link, do not frequently set rs = server. recordset ("adodb. recordset"), or set rs = nothing
The disadvantage is the traditional method for turning pages. Rs. absolutepage = rs. pagesize
----------------------------------------------------------
<%
'/****** Kshop ******/
'Adodb_class.asp database operation class

'Version 1.0
'Copyright [email] simple_1982@hotmail.com [/email]
'E-mail [email] xsg2005@163.com [/email]
'/*****************/
Class adodb_class
Dim conn, connstr, rs
Dim dbclass 'database type access, sql2000, sql2005 one of three values
Dim SqlDbName, SqlUser, SqlPass, SqlServer
Dim SqlAccess
Dim Selectd, Insertd, Updated
Dim rsCounts, rsPage, readCounts, and nowPage

Private Sub Class_Initialize ()
SqlDbName = ""
SqlUser = ""
SqlPass = ""
SqlServer = ""
SqlAccess = "/simple_date/simple_xiehui.mdb"
RsCounts = 0: rsPage = 1: readCounts = 0: nowPage = 1
Call OpenConn ("access ")

Selectd = 0
Insertd = 0
Updated = 0
End Sub
* Open the database link ******************
Private Sub AccessConn ()
Connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & server. mappath (SqlAccess) & "; Persist Security Info = False"
End Sub
Private Sub Sql2kConn ()
Connstr = "driver = {SQL server}; server =" & SqlServer & "; uid =" & SqlUser & "; pwd =" & SqlPass & "; database =" & SqlDbName
End Sub
Private Sub Sql2k05Conn ()
Connstr = "Provider = SQLNCLI.1; Password =" & SqlPass & "; User ID =" & SqlUser & "; Initial Catalog =" & SqlDbName & "; Data Source =" & SqlServer
End Sub
Private Sub OpenConn (db_class)
Dbclass = db_class
Select case db_class
Case "access": call AccessConn ()
Case "sql2000": call Sql2kConn ()
Case "sql2005": call Sql2k05Conn ()
End select
On error resume next
Set conn = server. CreateObject ("adodb. Connection ")
Conn. open connstr
If err then
Response. write "database link failed <br> sqlstring =" + connstr
Response. End ()
Err. clear
End if
End Sub
*************
Public Function Select_Table (SQL)
If not isempty (conn) or isnull (conn) then
Call OpenConn (dbclass)
Elseif conn. state = 0 then
Call OpenConn (dbclass)
End if
On error resume next
Set rs = Server. CreateObject ("adodb. recordset ")
Rs. open SQL, conn, 1, 1
If err then
Select_Table = False
Rs. close
Exit Function
Err. clear
End If
If rs. eof and rs. bof then
Rs. close
Select_Table = false
Else
Select_Table = true
End If
ReadCounts = readCounts + 1
End Function
'Pagination
Public Function Select_page (page, psize)
If isnull (page) or page = "" then page = 1
If page <1 then page = 1
If rs. state = 1 then
If not rs. eof then
Rs. pagesize = psize
RsPage = rs. pagecount
RsCounts = rs. recordcount
If int (page)> Int (rsPage) then page = rsPage
Rs. absolutepage = page: nowPage = page
End if
End if
End Function
'Update record
Public Function Update_Table (SQL)
If not isempty (conn) or isnull (conn) then
Call OpenConn (dbclass)
Elseif conn. state = 0 then
Call OpenConn (dbclass)
End if
On error resume next
If SQL <> "" then
Conn. Execute SQL, Updated
If err then
Update_Table = false
Err. clear
Else
Update_Table = true
End if
Else
Update_Table = false
End if
Conn. close
Set conn = nothing
End Function
'Add
'Input: insert SQL statement, table name, auto-increment Field
Public Function Insert_Table (SQL, table, id_column)
If not isempty (conn) or isnull (conn) then
Call OpenConn (dbclass)
Elseif conn. state = 0 then
Call OpenConn (dbclass)
End if
On error resume next
If SQL <> "" then
Conn. Execute (SQL)
If err then
Insert_Table = false: err. clear
Else
Insert_Table = true
End if
'Get the last added ID
If table <> "" and id_column <> "" then
Set ds = conn. Execute ("select" & id_column & "from" & table & "order by" & id_column & "desc ")
End if
If err then
Insertd = 0: err. clear
Else Insertd = ds (0)
End if
Set ds = nothing
Closed ()
Else
Insert_Table = false
End if
End Function
'Close the database link
Public Function closed ()
If not isempty (rs) and not isnull (rs) then
If rs. state = 1 then
Rs. close
End if
End if
RsCounts = 0: rsPage = 1: nowPage = 1
End function
************
Private Sub Class_Terminate ()
ReadCounts = 0: rsCounts = 0: rsPage = 0
If not isempty (conn) and not isnull (conn) then
If conn. state = 1 then
Conn. close
End if
Set conn = nothing
End if

If not isempty (rs) then
If not isnull (rs) then
If rs. state = 1 then
Rs. close
End if
Set rs = nothing
End if
End if
End Sub
End Class
%>

Related Article

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.