万能数据库连接程序
程序员文章站
2024-01-14 08:39:58
简介: 连接各种类型数据库 及 对数据库操作的函数 下面这部分程序可说是万能的数据库连接程序几乎可以连接所有的ms数据库,自己拿去研究吧(这个程序是“asp网页制作教程”这...
简介: 连接各种类型数据库 及 对数据库操作的函数
下面这部分程序可说是万能的数据库连接程序几乎可以连接所有的ms数据库,自己拿去研究吧(这个程序是“asp网页制作教程”这本书里面的——一本好书):
<%
'---------------------------------------------------
function getmdbconnection( filename )
dim provider, dbpath
provider = "provider=microsoft.jet.oledb.4.0;"
dbpath = "data source=" & server.mappath(filename)
set getmdbconnection = getconnection( provider & dbpath )
end function
'---------------------------------------------------
function getsecuredmdbconnection( filename, password )
dim provider, dbpath
provider = "provider=microsoft.jet.oledb.4.0;"
dbpath = "data source=" & server.mappath(filename)
set getsecuredmdbconnection = getconnection( provider & dbpath & ";jet oledb:database password=" & password ) end function
'---------------------------------------------------
function getdbcconnection( filename )
dim driver, sourcetype, dbpath
driver = "driver={microsoft visual foxpro driver};"
sourcetype = "sourcetype=dbc;"
dbpath = "sourcedb=" & server.mappath( filename )
set getdbcconnection = getconnection( driver & sourcetype & dbpath )
end function
'---------------------------------------------------
function getdbfconnection( directory )
dim driver, sourcetype, dbpath
driver = "driver={microsoft visual foxpro driver};"
sourcetype = "sourcetype=dbf;"
dbpath = "sourcedb=" & server.mappath( directory )
set getdbfconnection = getconnection( driver & sourcetype & dbpath )
end function
'---------------------------------------------------
function getexcelconnection( filename )
dim driver, dbpath
driver = "driver={microsoft excel driver (*.xls)};"
dbpath = "dbq=" & server.mappath( filename )
set getexcelconnection = getconnection( driver & "readonly=0;" & dbpath ) end function
'---------------------------------------------------
function gettextconnection( directory )
dim driver, dbpath
driver = "driver={microsoft text driver (*.txt; *.csv)};"
dbpath = "dbq=" & server.mappath( directory )
set gettextconnection = getconnection( driver & dbpath )
end function
'---------------------------------------------------
function getsqlserverconnection( computer, userid, password, db )
dim params, conn
set getsqlserverconnection = nothing
params = "provider=sqloledb.1"
params = params & ";data source=" & computer
params = params & ";user id=" & userid
params = params & ";password=" & password
params = params & ";initial catalog=" & db
set conn = server.createobject("adodb.connection")
conn.open params
set getsqlserverconnection = conn
end function
'---------------------------------------------------
function getmdbrecordset( filename, source )
set getmdbrecordset = getmdbrs( filename, source, 2, "" )
end function
'---------------------------------------------------
function getmdbstaticrecordset( filename, source )
set getmdbstaticrecordset = getmdbrs( filename, source, 3, "" )
end function
'---------------------------------------------------
function getsecuredmdbrecordset( filename, source, password )
set getsecuredmdbrecordset = getmdbrs( filename, source, 2, password ) end function
'---------------------------------------------------
function getsecuredmdbstaticrecordset( filename, source, password )
set getsecuredmdbstaticrecordset = getmdbrs( filename, source, 3, password ) end function
'---------------------------------------------------
function getdbfrecordset( directory, sql )
set getdbfrecordset = getotherrs( "dbf", directory, sql, 2 )
end function
'---------------------------------------------------
function getdbfstaticrecordset( directory, sql )
set getdbfstaticrecordset = getotherrs( "dbf", directory, sql, 3 )
end function
'---------------------------------------------------
function getdbcrecordset( filename, sql )
set getdbcrecordset = getotherrs( "dbc", filename, sql, 2 )
end function
'---------------------------------------------------
function getdbcstaticrecordset( filename, sql )
set getdbcstaticrecordset = getotherrs( "dbc", filename, sql, 3 )
end function
'---------------------------------------------------
function getexcelrecordset( filename, sql )
set getexcelrecordset = getotherrs( "excel", filename, sql, 2 )
end function
'---------------------------------------------------
function getexcelstaticrecordset( filename, sql )
set getexcelstaticrecordset = getotherrs( "excel", filename, sql, 3 )
end function
'---------------------------------------------------
function gettextrecordset( directory, sql )
set gettextrecordset = getotherrs( "text", directory, sql, 2 )
end function
'---------------------------------------------------
function gettextstaticrecordset( directory, sql )
set gettextstaticrecordset = getotherrs( "text", directory, sql, 3 )
end function
'---------------------------------------------------
function getsqlserverrecordset( conn, source )
dim rs
set rs = server.createobject("adodb.recordset")
rs.open source, conn, 2, 2
set getsqlserverrecordset = rs
end function
'---------------------------------------------------
function getsqlserverstaticrecordset( conn, source )
dim rs
set rs = server.createobject("adodb.recordset")
rs.open source, conn, 3, 2
set getsqlserverstaticrecordset = rs
end function
'---------------------------------------------------
function getconnection( param )
dim conn
on error resume next
set getconnection = nothing
set conn = server.createobject("adodb.connection")
if err.number <> 0 then exit function
conn.open param
if err.number <> 0 then exit function
set getconnection = conn
end function
'---------------------------------------------------
function getmdbrs( filename, source, cursor, password )
dim conn, rs
on error resume next
set getmdbrs = nothing
if len(password) = 0 then
set conn = getmdbconnection( filename )
else
set conn = getsecuredmdbconnection( filename, password )
end if
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open source, conn, cursor, 2
if err.number <> 0 then exit function
set getmdbrs = rs
end function
'---------------------------------------------------
function getotherrs( datatype, path, sql, cursor )
dim conn, rs
on error resume next
set getotherrs = nothing
select case datatype
case "dbf"
set conn = getdbfconnection( path )
case "dbc"
set conn = getdbcconnection( path )
case "excel"
set conn = getexcelconnection( path )
case "text"
set conn = gettextconnection( path )
end select
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open sql, conn, cursor, 2
if err.number <> 0 then exit function
set getotherrs = rs
end function
'---------------------------------------------------
function getsqlserverrs( computer, userid, password, db, source, cursor )
dim conn, rs
on error resume next
set getsqlserverrs = nothing
set conn = getsqlserverconnection( computer, userid, password, db )
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open source, conn, cursor, 2
if err.number <> 0 then exit function
set getsqlserverrs = rs
end function
%>
使用方法是——复制下来存成一个文件,然后用#include “文件名”就可以调用里面的子程序了。
有什么问题可以一起探讨!!!
下面这部分程序可说是万能的数据库连接程序几乎可以连接所有的ms数据库,自己拿去研究吧(这个程序是“asp网页制作教程”这本书里面的——一本好书):
<%
'---------------------------------------------------
function getmdbconnection( filename )
dim provider, dbpath
provider = "provider=microsoft.jet.oledb.4.0;"
dbpath = "data source=" & server.mappath(filename)
set getmdbconnection = getconnection( provider & dbpath )
end function
'---------------------------------------------------
function getsecuredmdbconnection( filename, password )
dim provider, dbpath
provider = "provider=microsoft.jet.oledb.4.0;"
dbpath = "data source=" & server.mappath(filename)
set getsecuredmdbconnection = getconnection( provider & dbpath & ";jet oledb:database password=" & password ) end function
'---------------------------------------------------
function getdbcconnection( filename )
dim driver, sourcetype, dbpath
driver = "driver={microsoft visual foxpro driver};"
sourcetype = "sourcetype=dbc;"
dbpath = "sourcedb=" & server.mappath( filename )
set getdbcconnection = getconnection( driver & sourcetype & dbpath )
end function
'---------------------------------------------------
function getdbfconnection( directory )
dim driver, sourcetype, dbpath
driver = "driver={microsoft visual foxpro driver};"
sourcetype = "sourcetype=dbf;"
dbpath = "sourcedb=" & server.mappath( directory )
set getdbfconnection = getconnection( driver & sourcetype & dbpath )
end function
'---------------------------------------------------
function getexcelconnection( filename )
dim driver, dbpath
driver = "driver={microsoft excel driver (*.xls)};"
dbpath = "dbq=" & server.mappath( filename )
set getexcelconnection = getconnection( driver & "readonly=0;" & dbpath ) end function
'---------------------------------------------------
function gettextconnection( directory )
dim driver, dbpath
driver = "driver={microsoft text driver (*.txt; *.csv)};"
dbpath = "dbq=" & server.mappath( directory )
set gettextconnection = getconnection( driver & dbpath )
end function
'---------------------------------------------------
function getsqlserverconnection( computer, userid, password, db )
dim params, conn
set getsqlserverconnection = nothing
params = "provider=sqloledb.1"
params = params & ";data source=" & computer
params = params & ";user id=" & userid
params = params & ";password=" & password
params = params & ";initial catalog=" & db
set conn = server.createobject("adodb.connection")
conn.open params
set getsqlserverconnection = conn
end function
'---------------------------------------------------
function getmdbrecordset( filename, source )
set getmdbrecordset = getmdbrs( filename, source, 2, "" )
end function
'---------------------------------------------------
function getmdbstaticrecordset( filename, source )
set getmdbstaticrecordset = getmdbrs( filename, source, 3, "" )
end function
'---------------------------------------------------
function getsecuredmdbrecordset( filename, source, password )
set getsecuredmdbrecordset = getmdbrs( filename, source, 2, password ) end function
'---------------------------------------------------
function getsecuredmdbstaticrecordset( filename, source, password )
set getsecuredmdbstaticrecordset = getmdbrs( filename, source, 3, password ) end function
'---------------------------------------------------
function getdbfrecordset( directory, sql )
set getdbfrecordset = getotherrs( "dbf", directory, sql, 2 )
end function
'---------------------------------------------------
function getdbfstaticrecordset( directory, sql )
set getdbfstaticrecordset = getotherrs( "dbf", directory, sql, 3 )
end function
'---------------------------------------------------
function getdbcrecordset( filename, sql )
set getdbcrecordset = getotherrs( "dbc", filename, sql, 2 )
end function
'---------------------------------------------------
function getdbcstaticrecordset( filename, sql )
set getdbcstaticrecordset = getotherrs( "dbc", filename, sql, 3 )
end function
'---------------------------------------------------
function getexcelrecordset( filename, sql )
set getexcelrecordset = getotherrs( "excel", filename, sql, 2 )
end function
'---------------------------------------------------
function getexcelstaticrecordset( filename, sql )
set getexcelstaticrecordset = getotherrs( "excel", filename, sql, 3 )
end function
'---------------------------------------------------
function gettextrecordset( directory, sql )
set gettextrecordset = getotherrs( "text", directory, sql, 2 )
end function
'---------------------------------------------------
function gettextstaticrecordset( directory, sql )
set gettextstaticrecordset = getotherrs( "text", directory, sql, 3 )
end function
'---------------------------------------------------
function getsqlserverrecordset( conn, source )
dim rs
set rs = server.createobject("adodb.recordset")
rs.open source, conn, 2, 2
set getsqlserverrecordset = rs
end function
'---------------------------------------------------
function getsqlserverstaticrecordset( conn, source )
dim rs
set rs = server.createobject("adodb.recordset")
rs.open source, conn, 3, 2
set getsqlserverstaticrecordset = rs
end function
'---------------------------------------------------
function getconnection( param )
dim conn
on error resume next
set getconnection = nothing
set conn = server.createobject("adodb.connection")
if err.number <> 0 then exit function
conn.open param
if err.number <> 0 then exit function
set getconnection = conn
end function
'---------------------------------------------------
function getmdbrs( filename, source, cursor, password )
dim conn, rs
on error resume next
set getmdbrs = nothing
if len(password) = 0 then
set conn = getmdbconnection( filename )
else
set conn = getsecuredmdbconnection( filename, password )
end if
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open source, conn, cursor, 2
if err.number <> 0 then exit function
set getmdbrs = rs
end function
'---------------------------------------------------
function getotherrs( datatype, path, sql, cursor )
dim conn, rs
on error resume next
set getotherrs = nothing
select case datatype
case "dbf"
set conn = getdbfconnection( path )
case "dbc"
set conn = getdbcconnection( path )
case "excel"
set conn = getexcelconnection( path )
case "text"
set conn = gettextconnection( path )
end select
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open sql, conn, cursor, 2
if err.number <> 0 then exit function
set getotherrs = rs
end function
'---------------------------------------------------
function getsqlserverrs( computer, userid, password, db, source, cursor )
dim conn, rs
on error resume next
set getsqlserverrs = nothing
set conn = getsqlserverconnection( computer, userid, password, db )
if conn is nothing then exit function
set rs = server.createobject("adodb.recordset")
if err.number <> 0 then exit function
rs.open source, conn, cursor, 2
if err.number <> 0 then exit function
set getsqlserverrs = rs
end function
%>
使用方法是——复制下来存成一个文件,然后用#include “文件名”就可以调用里面的子程序了。
有什么问题可以一起探讨!!!
上一篇: jsp中自定义标签用法实例分析