Universal Database Connection Program _asp Foundation

Source: Internet
Author: User
Introduction: Connecting the various types of databases and operations on the database functions

The following part of the program can be said to be a universal database connector almost all of the MS Database can be connected to their own to study it (this program is "ASP Web page Production tutorial" In this book-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 & ";D ata 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
%>
Use the method--copy it down to save it as a file, and then use the #include "filename" to call the inside subroutine.
What questions can we discuss together!!!

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.