ASP 包含文件示例

<script language=vbscript runat=server>

function MakeConnection()
    set  MakeConnection = server.CreateObject("ADODB.connection")
end function

function MakeRecordset()
    set  MakeRecordset = server.CreateObject("ADODB.Recordset")
end function

function MakeCommand()
    set  MakeCommand = server.CreateObject("ADODB.Command")
end function

function MakeParameter()
    set  MakeParameter = server.CreateObject("ADODB.Parameter") 
end function

function ConnectMssqlLocal(database)
    dim cn
    set cn = MakeConnection()
    cn.ConnectionString = "DRIVER={SQL Server};"_
                        & "SERVER=" & "localhost" & ";"_
                        & "Trusted_Connection=yes;" & "DATABASE=" & database
    cn.open
    set ConnectMssqlWin = cn
end function

' 连接 SQL SERVER 2000 数据库
function ConnectMssql(serverid,database,userid,password)
    dim cn
    set cn = MakeConnection()
    cn.ConnectionString = "DRIVER={SQL Server};"_
                        & "SERVER=" & serverid & ";"_
                        & "user id=" & userid & ";"_
                        & "password=" & password & ";"
    if database<>"" then _
        cn.ConnectionString = cn.ConnectionString & "DATABASE=" & database
    cn.open
    set connectmssql=cn
end function

' 连接 SQL SERVER 2000,Windows 用户认证
function ConnectMssqlWin(serverid,database)
    dim cn
    set cn = MakeConnection()
    cn.ConnectionString = "DRIVER={SQL Server};"_
                        & "SERVER=" & serverid & ";"_
                        & "Trusted_Connection=yes;"
    if database<>"" then _
        cn.ConnectionString = cn.ConnectionString & "DATABASE=" & database
    response.write cn.ConnectionString
    cn.open
    set ConnectMssqlWin = cn
end function

' 连接到 MySql 数据库
function ConnectMysql(sv,data,user,pass)
    dim cn
    set cn = MakeConnection()
    cn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};"_
                        & "SERVER=" & sv & ";"_ 
                        & "DATABASE=" & data & ";"_
                        & "UID=" & user & ";" _
                        & "PWD=" & pass & ";OPTION=3"
    on error resume next
    cn.open
    if err.number>0 then
        response.write err.description & "<br><br>"
        response.write "无法连接数据库,请与管理员联系。"
    response.write "<br><br><a href='' onclick=window.history.back(-1)>点击返回上页</a>"
        response.end
    end if
    set connectmysql=cn
end function

' 只读连接到 EXCEL 文件
function ConnectExcelRead(database)

    dim cn
    set cn = MakeConnection()
    cn.open "driver={microsoft excel driver (*.xls)};dbq=" & database
    set connectexcelread = cn

end function

' 独占连接到 EXCEL 文件, 可更新
function ConnectExcel(database)
    dim cn
    set cn = MakeConnection()
    cn.open "Extended Properties='Excel 8.0';Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & database
    set connectexcel = cn
end function


'连接 ACCESS 数据库
function ConnectAccess(database)
    dim cn
    set cn = MakeConnection()
    cn.open "driver={microsoft access driver (*.mdb)};uid=tc;pwd=;dbq=" & database
    set connectaccess = cn
end function


'连接 DBase 目录, 表名为 DBF 文件的主名
function ConnectDBF(database)
    dim cn
    set cn = MakeConnection()
    cn.open "driver={microsoft dbase driver (*.dbf)};driverid=277;dbq=" & database
    set ConnectDBF = cn
end function


' 连接到 TXT 文件
function ConnectTxt(database)
    dim cn
    set cn = MakeConnection()
    cn.open "Driver={Microsoft Text Driver (*.txt; *.csv)};extensions=asc,csv,tab,txt;Persist SecurityInfo=false;dbq=" & database
    set connecttxt = cn
end function


'通过 FILEDSN 连接到数据库
function ConnectFileDSN(database)

    dim cn
    set cn = MakeConnection()
    on error resume next
    cn.open "FILEDSN=" & database
    if err.number>0 then
        response.write err.description & "<br><br>"
        response.write "文件 DSN 没有配置,无法连接数据库,请与管理员联系。"
        response.end
    end if
    set connectfiledsn = cn
end function


'通过 DSN 连接到数据库
function ConnectDSN(database)
    dim cn
    set cn = MakeConnection()
    on error resume next
    cn.open "DSN=" & database
    if err.number>0 then
        response.write err.description & "<br><br>"
        response.write "DSN 没有配置,无法连接数据库,请与管理员联系。"
        response.end
    end if
    set connectdsn = cn

end function


' 显示记录,表格形式
sub ShowRecordset(rec)
    if rec.eof then exit sub
    dim i,j
    response.write "<table border=1>"
    response.write vbcrlf
    response.write "<tr>"
    for i=0 to rec.fields.count-1
        response.write "<th>" & rec.fields(i).name & "</th>"
    next
    response.write "</tr>" & vbcrlf

    do while rec.eof=false
        response.write "  <tr>"
        for i=0 to rec.fields.count-1
            response.write "<td>" & replace(server.htmlencode(rec.fields(i).value & ""),chr(13) & chr(10),"<BR>") & "</td>"
        next    
        response.write "</tr>" & vbcrlf
        rec.movenext
    loop
    response.write "</table>" & vbcrlf
end sub


sub makealert(info)
    response.write "<" & "script language=javascript>"
    response.write "alert('" & replace(info,"'","\'") & "');"
    response.write "<" & "/script>"
    response.write vbcrlf
end sub


function getint(s)
    on error resume next
    getint=0
    getint=cint(s)
end function


function getsqltext(s)
    getsqltext = replace(s,"'","''")
end function

</script>

1111