开店乐

开店乐电子商务研究 KaiDianLe.Com

网站地图 :

  搜索:

一个普通的数据库例子源源程序


     To assist in interfacing with databases. This script can format variables and return SQL formats.
Such as double quoting apposterphies and surrounding strings with quotes, Returning NULL for invalid data
types, trimming strings so they do not exceed maximum lengths. This also has some functions so that you
can open and close databases more conveiently with just one line of code. You can query a database and get
an Array as well with some code.


  
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!



    '**************************************
    ' for :Common Database Routines
    '**************************************
    Copyright (c) 1999 by Lewis Moten, All rights reserved.


code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!



    '**************************************
    ' Name: Common Database Routines
    ' Description:To assist in interfacing w
    '     ith databases. This script can format va
    '     riables and return SQL formats. Such as
    '     double quoting apposterphies and surroun
    '     ding strings with quotes, Returning NULL
    '     for invalid data types, trimming strings
    '     so they do not exceed maximum lengths. T
    '     his also has some functions so that you
    '     can open and close databases more convei
    '     ently with just one line of code. You ca
    '     n query a database and get an Array as w
    '     ell with some code.
    ' By: Lewis Moten
    '
    '
    ' Inputs:None
    '
    ' Returns:None
    '
    'Assumes:This script assumes that you at
    '     least have Microsoft ActiveX Data Object
    '     s 2.0 or Higher (ADODB). This script may
    '     get some getting used to at first until
    '     you go through and study what each routi
    '     ne can do.
    '
    'Side Effects:None
    '
    'Warranty:
    'code provided by Planet Source Code(tm)
    '     (www.Planet-Source-Code.com) 'as is', wi
    '     thout warranties as to performance, fitn
    '     ess, merchantability,and any other warra
    '     nty (whether expressed or implied).
    'Terms of Agreement:
    'By using this source code, you agree to
    '     the following terms...
    ' 1) You may use this source code in per
    '     sonal projects and may compile it into a
    '     n .exe/.dll/.ocx and distribute it in bi
    '     nary format freely and with no charge.
    ' 2) You MAY NOT redistribute this sourc
    '     e code (for example to a web site) witho
    '     ut written permission from the original
    '     author.Failure to do so is a violation o
    '     f copyright laws.
    ' 3) You may link to this code from anot
    '     her website, provided it is not wrapped
    '     in a frame.
    ' 4) The author of this code may have re
    '     tained certain additional copyright righ
    '     ts.If so, this is indicated in the autho
    '     r's description.
    '**************************************
    
    <!--METADATA Type="TypeLib" NAME="Microsoft ActiveX Data Objects 2.0 Library" UUID="{00000200-0000-
0010-8000-00AA006D2EA4}" VERSION="2.0"-->
    <%
    ' Setup the ConnectionString
    Dim sCONNECTION_STRING
    sCONNECTION_STRING = "DRIVER=Microsoft Access Driver
(*.mdb);DBQ=D:\inetpub\wwwroot\inc\data\database.mdb;"
    Dim oConn
    '---------------------------------------
    '     ----------------------------------------
    '     
    Function DBConnOpen(ByRef aoConnObj)
     ' This routine connects To a database and returns
     ' weather or Not it was successful
     ' Prepare For any errors that may occur While connecting To the database
     On Error Resume Next
     ' Create a connection object
     Set aoConnObj = Server.CreateObject("ADODB.Connection")
     ' Open a connection To the database
     Call aoConnObj.Open(sCONNECTION_STRING)
     ' If any errors have occured
     If Err Then
     ' Clear errors
     Err.Clear
     ' Release connection object
     Set aoConnObj = Nothing
     ' Return unsuccessful results
     DBConnOpen = False
     ' Else errors did Not occur
     Else
     ' Return successful results
     DBConnOpen = True
     End If ' Err
    End Function ' DBConnOpen
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function DBConnClose(ByRef aoConnObj)
     ' This routine closes the database connection and releases objects
     ' from memory
     ' If the connection variable has been defined as an object
     If IsObject(aoConnObj) Then
     ' If the connection is open
     If aoConnObj.State = adStateOpen Then
     ' Close the connection
     aoConnObj.Close
     ' Return positive Results
     DBConnClose = True
     End If ' aoConnObj.State = adStateOpen
     ' Release connection object
     Set aoConnObj = Nothing
     End If ' IsObject(aoConnObj)
    End Function ' DBConnClose
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SetData(ByRef asSQL, ByRef avDataAry)
     ' This routine acquires data from the database
     Dim loRS ' ADODB.Recordset Object
     ' Create Recordset Object
     Set loRS = Server.CreateObject("ADODB.Recordset")
     ' Prepare For errors when opening database connection
     On Error Resume Next
     ' If a connection object has been defined
     If IsObject(oConn) Then
     ' If the connection is open
     If oConn.State = adStateOpen Then
     ' Acquire data With connection object
     Call loRS.Open(asSQL, oConn, adOpenForwardOnly, adLockReadOnly)
     ' Else the connection is closed
     Else
     ' Set the ConnectionString
     Call SetConnectionString(csConnectionString)
     ' If atempt To open connection succeeded
     If DBConnOpen() Then
     ' Acquire data With connection object
     Call loRS.Open(asSQL, oConn, adOpenForwardOnly, adLockReadOnly)
     ' Return connection object To closed state
     Call DBConnClose()
     End If ' DBConnOpen()
     End If ' aoConn.State = adStateOpen
     ' Else active connection is the ConnectionString
     Else
     ' Acquire data With ConnectionString
     Call loRS.Open(asSQL, sCONNECTION_STRING, adOpenForwardOnly, adLockReadOnly)
     End If ' IsObject(oConn)
     ' If errors occured
     If Err Then
     response.write "<HR color=red>" & err.description & "<HR color=red>" & asSQL & "<HR
color=red>"
     ' Clear the Error
     Err.Clear
     ' If the recorset is open
     If loRS.State = adStateOpen Then
     ' Close the recorset
     loRS.Close
     End If ' loRS.State = adStateOpen
     ' Release Recordset from memory
     Set loRS = Nothing
     ' Return negative results
     SetData = False
     ' Exit Routine
     Exit Function
     End If ' Err
     ' Return positve results
     SetData = True
     ' If data was found
     If Not loRS.EOF Then
     ' Pull data into an array
     avDataAry = loRS.GetRows
     End If ' Not loRS.EOF
     ' Close Recordset
     loRS.Close
     ' Release object from memory
     Set loRS = Nothing
    End Function ' SetData
    '---------------------------------------
    '     ----------------------------------------
    '     
    ' SQL Preperations are used to prepare v
    '     ariables for SQL Queries. If
    ' invalid data is passed to these routin
    '     es, NULL values or Default Data
    ' is returned to keep your SQL Queries f
    '     rom breaking from users breaking
    ' datatype rules.
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SQLPrep_s(ByVal asExpression, ByRef anMaxLength)
     ' If maximum length is defined
     If anMaxLength > 0 Then
     ' Trim expression To maximum length
     asExpression = Left(asExpression, anMaxLength)
     End If ' anMaxLength > 0
     ' Double quote SQL quote characters
     asExpression = Replace(asExpression, "'", "''")
     ' If Expression is Empty
     If asExpression = "" Then
     ' Return a NULL value
     SQLPrep_s = "NULL"
     ' Else expression is Not empty
     Else
     ' Return quoted expression
     SQLPrep_s = "'" & asExpression & "'"
     End If ' asExpression
    End Function ' SQLPrep_s
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SQLPrep_n(ByVal anExpression)
     ' If expression numeric
     If IsNumeric(anExpression) And Not anExpression = "" Then
     ' Return number
     SQLPrep_n = anExpression
     ' Else expression Not numeric
     Else
     ' Return NULL
     SQLPrep_n = "NULL"
     End If ' IsNumeric(anExpression) And Not anExpression = ""
    End Function ' SQLPrep_n
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SQLPrep_b(ByVal abExpression, ByRef abDefault)
     ' Declare Database Constants
     Const lbTRUE = -1 '1 = SQL, -1 = Access
     Const lbFALSE = 0
     Dim lbResult ' Result To be passed back
     ' Prepare For any errors that may occur
     On Error Resume Next
     ' If expression Not provided
     If abExpression = "" Then
     ' Set expression To default value
     abExpression = abDefault
     End If ' abExpression = ""
     ' Attempt To convert expression
     lbResult = CBool(abExpression)
     ' If Err Occured
     If Err Then
     ' Clear the Error
     Err.Clear
     ' Determine action based on Expression
     Select Case LCase(abExpression)
     ' True expressions
     Case "yes", "on", "true", "-1", "1"
     lbResult = True
     ' False expressions
     Case "no", "off", "false", "0"
     lbResult = False
     ' Unknown expression
     Case Else
     lbResult = abDefault
     End Select ' LCase(abExpression)
     End If ' Err
     ' If result is True
     If lbResult Then
     ' Return True
     SQLPrep_b = lbTRUE
     ' Else Result is False
     Else
     ' Return False
     SQLPrep_b = lbFALSE
     End If ' lbResult
    End Function ' SQLPrep_b
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SQLPrep_d(ByRef adExpression)
     ' If Expression valid Date
     If IsDate(adExpression) Then
     ' Return Date
     'SQLPrep_d = "'" & adExpression & "'" ' SQL Database
     SQLPrep_d = "#" & adExpression & "#" ' Access Database
     ' Else Expression Not valid Date
     Else
     ' Return NULL
     SQLPrep_d = "NULL"
     End If ' IsDate(adExpression)
    End Function ' SQLPrep_d
    '---------------------------------------
    '     ----------------------------------------
    '     
    Public Function SQLPrep_c(ByVal acExpression)
     ' If Empty Expression
     If acExpression = "" Then
     ' Return Null
     SQLPrep_c = "NULL"
     ' Else expression has content
     Else
     ' Prepare For Errors
     On Error Resume Next
     ' Attempt To convert expression to Currency
     SQLPRep_c = CCur(acExpression)
     ' If Error occured
     If Err Then
     ' Clear Error
     Err.Clear
     SQLPrep_c = "NULL"
     End If ' Err
     End If ' acExpression = ""
    End Function ' SQLPrep_c
    '---------------------------------------
    '     ----------------------------------------
    '     
    Function buildJoinStatment(sTable,sFldLstAry,rs,conn)
    Dim i,sSql,sTablesAry,sJnFldsAry,bJoinAry,sJoinDisplay
    ReDim sTablesAry(UBound(sFldLstAry))
    ReDim sJnFldsAry(UBound(sFldLstAry))
    ReDim bJoinAry(UBound(sFldLstAry))
    For i = 0 To UBound(sFldLstAry)
    sSql = "SELECT OBJECT_NAME(rkeyid),COL_NAME(rkeyid,rkey1)"
    sSql = sSql &" FROM sysreferences"
    sSql = sSql &" WHERE fkeyid = OBJECT_ID('"& sTable &"') "
    sSql = sSql &" AND col_name(fkeyid,fkey1) = '"& Trim(sFldLstAry(i)) &"'"
    rs.open sSql,conn
    If Not rs.eof Then
    sTablesAry(i) = rs(0)
    sJnFldsAry(i) = rs(1)
    End If
    rs.close
    Next
    If UBound(sFldLstAry) >= 0 Then
    For i = 0 To UBound(sFldLstAry)
    If sTablesAry(i) <> "" Then
    bJoinAry(i) = True
    Else
    bJoinAry(i) = False
    End If
    If i <> UBound(sFldLstAry) Then sSql = sSql &" +' - '+ "
    Next
    sSql = "FROM "& sTable
    For i = 0 To UBound(sFldLstAry)
    If bJoinAry(i) Then sSql = sSql &" LEFT JOIN "& sTablesAry(i) &" ON "& sTable &"."& sFldLstAry(i) &"
= "& sTablesAry(i) &"."& sJnFldsAry(i)
    Next
    End If
    buildJoinStatment = sSql
    End Function
    '---------------------------------------
    '     ----------------------------------------
    '     
    Function buildQuery(ByRef asFieldAry, ByVal asKeyWords)
     ' To find fields that may have a word in them
     ' OR roger
     ' roger
     ' roger
     ' To find fields that must match a word
     ' AND roger
     ' + roger
     ' & roger
     ' To find fields that must Not match a word
     ' Not roger
     ' - roger
     ' Also use phrases
     ' +"rogers dog" -cat
     ' +(rogers dog)
     Dim loRegExp
     Dim loRequiredWords
     Dim loUnwantedWords
     Dim loOptionalWords
     Dim lsSQL
     Dim lnIndex
     Dim lsKeyword
     Set loRegExp = New RegExp
     loRegExp.Global = True
     loRegExp.IgnoreCase = True
     loRegExp.Pattern = "((AND[+&])\s*[\(\[\{""].*[\)\]\}""])((AND\s[+&])\s*\b[-\w']+\b)"
     Set loRequiredWords = loRegExp.Execute(asKeywords)
     asKeywords = loRegExp.Replace(asKeywords, "")
     loRegExp.Pattern = "(((NOT[-])\s*)?[\(\[\{""].*[\)\]\}""])(((NOT\s+[-])\s*)\b[-\w']+\b)"
     Set loUnwantedWords = loRegExp.Execute(asKeywords)
     asKeywords = loRegExp.Replace(asKeywords, "")
     loRegExp.Pattern = "(((OR[])\s*)?[\(\[\{""].*[\)\]\}""])(((OR\s+[])\s*)?\b[-\w']+\b)"
     Set loOptionalWords = loRegExp.Execute(asKeywords)
     asKeywords = loRegExp.Replace(asKeywords, "")
     If Not loRequiredWords.Count = 0 Then
     ' REQUIRED
     lsSQL = lsSQL & "("
     For lnIndex = 0 To loRequiredWords.Count - 1
     lsKeyword = loRequiredWords.Item(lnIndex).Value
     loRegExp.Pattern = "^(AND[+&])\s*"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     loRegExp.Pattern = "[()""\[\]{}]"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     lsKeyword = Replace(lsKeyword, "'", "''")
     If Not lnIndex = 0 Then
     lsSQL = lsSQL & " AND "
       End If
     lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ")
& " LIKE '%" & lsKeyword & "%')"
     Next
     lsSQL = lsSQL & ")"
     End If
     If Not loOptionalWords.Count = 0 Then
     ' OPTIONAL
     If lsSQL = "" Then
     lsSQL = lsSQL & "("
     Else
     lsSQL = lsSQL & " AND ("
     End If
     For lnIndex = 0 To loOptionalWords.Count - 1
     lsKeyword = loOptionalWords.Item(lnIndex).Value
     loRegExp.Pattern = "^(OR[])\s*"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     loRegExp.Pattern = "[()""\[\]{}]"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     lsKeyword = Replace(lsKeyword, "'", "''")
     If Not lnIndex = 0 Then
     lsSQL = lsSQL & " OR "
     End If
     lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ")
& " LIKE '%" & lsKeyword & "%')"
     Next
     lsSQL = lsSQL & ")"
     End If
     If Not loUnwantedWords.Count = 0 Then
     ' UNWANTED
     If lsSQL = "" Then
     lsSQL = lsSQL & "NOT ("
     Else
     lsSQL = lsSQL & " AND Not ("
     End If
     For lnIndex = 0 To loUnwantedWords.Count - 1
     lsKeyword = loUnWantedWords.Item(lnIndex).Value
     loRegExp.Pattern = "^(NOT[-])\s*"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     loRegExp.Pattern = "[()""\[\]{}]"
     lsKeyword = loRegExp.Replace(lsKeyword, "")
     lsKeyword = Replace(lsKeyword, "'", "''")
     If Not lnIndex = 0 Then
     lsSQL = lsSQL & " OR "
     End If
     lsSQL = lsSQL & "(" & Join(asFieldAry, " LIKE '%" & lsKeyword & "%' OR ")
& " LIKE '%" & lsKeyword & "%')"
     Next
     lsSQL = lsSQL & ")"
     End If
     If Not lsSQL = "" Then lsSQL = "(" & lsSQL & ")"
     buildQuery = lsSQL
    End Function
    '---------------------------------------
    '     ----------------------------------------
    '     
    %>

【日期:2006-8-5】【作者:不祥】【转载自:开店乐】

相关文章:
 最好的网上开店系统:凡人网络购物系统免费下载
 Rs.open sql,conn,A,B 的A、B各代表什么?
 ASP开发中存储过程应用全接触
 Oracle大文本在ASP中存取问题的解决
 数据分页方法新思路,速度非常快!
 ASP+vbscript写的万能查询表达式生成器
 常用网站数据库SQL操作语句
 ASP程序与SQL存储过程详解
 ASP脚本一空间绑定多个域名代码
 WEB编程开发常用的代码大全
 解决大字段在Form中Post出错的方法
 学习ASP之编写安全的ASP代码
 ASP程序应用之模板采用
 防止别人批量采集功能的ASP代码
 网页图片下拉选择控件使用实例
 平时写程序的时候出错时的解决方法
 “在线访客”的制作方法
 ASP中数据库调用时常见错误的现象和解决
 ASP 编程中20个非常有用的例子
 经典实用的基础asp程序整理
 ASP中从数据库读取二进制文件数据代码
 ASP动态生成的javascript表单验证代码
 在电子商务中实现购物车的方法
 ASP利用Google实现在线翻译功能
 实现千万级数据分页的存储过程
 详细说明用ASP和WML来实现数据库查询
 ASP访问INTERBASE数据库
 ASP安全配置不完全手册
 在ASP中如何访问Novell下的数据库
 ASP进阶学习必经之认识数学函数11种
 初学者必读 ASP运行环境的搭建
 解析asp的脚本语言
 学习使用ASP对象和组件
 让ASP程序运行于非Windows平台
 通过启动脚本来感受ASP的力量
 一些不长见的ASP调用存储过程的技巧
 使用ASP脚本技术
 优化Web数据库页面
 Asp限制IP访问代码
 ACCESS数据库防下载另类方法
 ASP浏览器性能组件
 细说ASP中Counters 组件
 全面解析Server对象
 ASP 内建对象Request和Respones
 深入研究Application和Session对象
 使用ASP、VB和XML建立运行于互联网上的应用程序
 在客户端执行数据库记录的分页显示
 对ASP脚本源代码进行加密
 用代码打开Access文件的两种方法
 使用Visual InterDev进行小组开发
 用JScript脚本实现分页的另类办法
 ASP中Cookie读写的实现方法
 如何使用ASP建立虚拟的FTP服务器
 在ASP中自动创建多级文件夹的函数
 一个硬盘文件搜索的Asp源码
 ASP使用MYSQL数据库全攻略
 ASP上传数据流格式分析详解
 ASP汉字转换UTF-8及UTF-8转换GB2312
 ASP常用数据库连接及操作的方法
 ASP编程中常用SQL命令使用方法
 ASP查询记录时RecordCount=-1问题
 让你的WAP网站有更好的兼容性
 如何注册服务器端组件
 轻松实现任何程序和动易整合
 在服务器端调用winzip命令行对上传的多个文件打包压缩
 用ASP制作强大的搜索引擎
 ASP彩色校验码的制作
 ASP 系列函数大全
 ASP程序处理进程进度条
 Asp无组件生成缩略图
 用ASP实现自动建站.实现虚拟二级目录
 删除Access数词库中的空记录
 ASP身份证验证代码函数
 ASP写的自动生成SELECT表单的函数
 几种打开记录集方式的比较
 用ASP实现汉字转拼音的功能
 ASP分页代码,已经写成类了,值得参考
 ASP下载系统防盗链方法
 Global.asa文件用法大全
 如何防止页面中的敏感信息被提取
 Delphi编写组件封装asp代码的基本步骤
 制做行背景颜色交替变换的表格
 如何用foreach遍历页面上所有的TextBox
 将数据库中的信息存储至XML文件中
 用Asp写个加密和解密的类
 如何固定表格的标题行和标题列
 ASP小偷(远程数据获取)程序入门教程
 Asp编写不再让人讨厌的自动弹出窗口
 用ASP实现在线压缩与解压缩
 使用组件封装ASP的数据库操作
 ASP中读写注册表
 ASP判断函数一览及网页制作常用技术
 ASP中Cookie使用指南
 随机产生用户密码(good)
 ASP:如何对身份证的籍贯进行验证
 ASP产生随机密码的函数
 ASP+ADO实现数据读写简单示例
 一个简单的用户登录接口ASP实现
 ASP+SQL Server构建网页防火墙
 一个通用的保护ASP系统的方法
 利用ASP发送和接收XML数据的处理方法

版权所有:Kaidianle.Com  联系方式:Shnxn@Yhaoo.Com.Cn 京ICP备06028743号 在线留言