Universal Database Connection Program

Source: Internet
Author: User

Introduction: connecting to various types of databases and database operation functions

BelowProgramIt can be said that the omnipotent database connection program can connect almost all the MS databases, and you can study it yourself (this program is a good book in the book "asp web page creation tutorial"-a good book ):
<%
'---------------------------------------------------
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
%>
The usage is -- copy and save it as a file, and then use # include "file name" to call the subroutine in it.
Can you discuss any issues !!!

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.