数据库连接大全

数据库连接大全,第1张

概述无意中在公司的电脑里发现前人留下的一个db.fun的文件,打开一看原来是众多连接数据库的函数。也许用得着,收录一下。 <%'---------------------------------------------------Function GetMdbConnection( FileN...

无意中在公司的电脑里发现前人留下的一个db.fun的文件,打开一看原来是众多连接数据库的函数。也许用得着,收录一下。

<%
'---------------------------------------------------
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,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,3,"" )
End Function

'---------------------------------------------------
Function GetSecuredMdbRecordset( filename,Password )
   Set GetSecuredMdbRecordset = GetMdbRs( filename,Password )
End Function

'---------------------------------------------------
Function GetSecuredMdbStaticRecordset( filename,Password )
   Set GetSecuredMdbStaticRecordset = GetMdbRs( filename,Password )
End Function

'---------------------------------------------------
Function GetDbfRecordset( Directory,sql )
   Set GetDbfRecordset = GetotherRs( "Dbf",Directory,sql,2 )
End Function

'---------------------------------------------------
Function GetDbfStaticRecordset( Directory,sql )
   Set GetDbfStaticRecordset = GetotherRs( "Dbf",3 )
End Function

'---------------------------------------------------
Function GetDbcRecordset( filename,sql )
   Set GetDbcRecordset = GetotherRs( "Dbc",filename,2 )
End Function

'---------------------------------------------------
Function GetDbcStaticRecordset( filename,sql )
   Set GetDbcStaticRecordset = GetotherRs( "Dbc",3 )
End Function

'---------------------------------------------------
Function GetExcelRecordset( filename,sql )
   Set GetExcelRecordset = GetotherRs( "Excel",2 )
End Function

'---------------------------------------------------
Function GetExcelStaticRecordset( filename,sql )
   Set GetExcelStaticRecordset = GetotherRs( "Excel",3 )
End Function

'---------------------------------------------------
Function GetTextRecordset( Directory,sql )
   Set GetTextRecordset = GetotherRs( "Text",2 )
End Function

'---------------------------------------------------
Function GetTextStaticRecordset( Directory,sql )
   Set GetTextStaticRecordset = GetotherRs( "Text",3 )
End Function

'---------------------------------------------------
Function GetsqlServerRecordset( conn,source )
   Dim rs

   Set rs = Server.CreateObject("ADODB.Recordset")
   rs.Open source,conn,2
   Set GetsqlServerRecordset = rs
End Function

'---------------------------------------------------
Function GetsqlServerStaticRecordset( conn,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,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,2
   If Err.Number <> 0 Then Exit Function
   Set GetMdbRs = rs
End Function

'---------------------------------------------------
Function GetotherRs( DataType,Path,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,2
   If Err.Number <> 0 Then Exit Function
   Set GetotherRs = rs
End Function

'---------------------------------------------------
Function GetsqlServerRs( Computer,Db,source,rs

   On Error Resume Next
   Set GetsqlServerRs = nothing
   Set conn = GetsqlServerConnection( Computer,Db )
   If conn Is nothing Then Exit Function
  
   Set rs = Server.CreateObject("ADODB.Recordset")
   If Err.Number <> 0 Then Exit Function
  
   rs.Open source,2
   If Err.Number <> 0 Then Exit Function
  
   Set GetsqlServerRs = rs
End Function
%>

总结

以上是内存溢出为你收集整理的数据库连接大全全部内容,希望文章能够帮你解决数据库连接大全所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1210792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存