好东西不敢独享,大家都来吧!不用DNS连接常用数据库的万能代码!!!!

funboy88 2002-06-06 01:10:47
----转贴自网易社区精华区

我从来就不用DNS连接,我觉得那个很不方便,下面这部分程序可说是万能的数据库连接程序几乎可以连接所有的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 “文件名”就可以调用里面的子程序了。
有什么问题可以一起探讨!!
...全文
104 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
sintian 2002-06-06
  • 打赏
  • 举报
回复
还行吧
julyclyde 2002-06-06
  • 打赏
  • 举报
回复
数据库类型是无限的
但是程序是有限的

用有限的代码无法应付无限多的数据库
linzhisong 2002-06-06
  • 打赏
  • 举报
回复
呵呵
limpid 2002-06-06
  • 打赏
  • 举报
回复
是啊,有些人就是太自大了
「已注销」 2002-06-06
  • 打赏
  • 举报
回复
没什么可说的,谢谢。
大家都在学习,何必说那写无聊的话,打击大家的积极性
sduwjr 2002-06-06
  • 打赏
  • 举报
回复
感谢,感谢
simpleli 2002-06-06
  • 打赏
  • 举报
回复
我有這本書,書上輔帶的光盤就有這個,這本書我覺得還可以吧,不過不要指望可以從書上得到什么好的東西,都是入門的東東來的.
funboy88 2002-06-06
  • 打赏
  • 举报
回复
并不是叫大家把这么长的代码加到程序中的呀,这只是一个多种数据库连接的字符串的参考,你完全可以分离开来呀,这两天我遇到一个ASP和FOXPRO的连接问题,还有PHP和FOXPRO的连接问题,不用DSN的方法,找了好长时间才解决
不 容易,就发给大家了哟,
想收藏就收藏呀,我没有逼你非要看呀,
BrightEye 2002-06-06
  • 打赏
  • 举报
回复
参考!
ruirui521 2002-06-06
  • 打赏
  • 举报
回复
对于新手来说可以看看
一杯咖啡 2002-06-06
  • 打赏
  • 举报
回复
"我从来就不用DNS连接,我觉得那个很不方便,"
我们个人自己编一些程序不用DNS连接可能是很方便,
但在一个安全要求较高的项目这样可就不行了。
如果你的ASP源码暴露了,那你的数据库就相对不安全了。
eshao 2002-06-06
  • 打赏
  • 举报
回复
不屑一顾的人去死哈!
高手还不是从低手变成的
别人好心贴,还这么多废话
kanghl 2002-06-06
  • 打赏
  • 举报
回复
连什么用什么串不就行了
ssm1226 2002-06-06
  • 打赏
  • 举报
回复
同情
水墨画 2002-06-06
  • 打赏
  • 举报
回复
不错
andysern 2002-06-06
  • 打赏
  • 举报
回复
UP
Lostinet 2002-06-06
  • 打赏
  • 举报
回复
真的太无聊了。
连接数据库又不是什么大问题。
干什么搞这么长的代码?
funboy88 2002-06-06
  • 打赏
  • 举报
回复
to julyclyde(争取下次的MVP) :
不要这么说呀,你的水平很高,像我们这些低手只有用这些垃圾了哟,

自以为是
smile_larry 2002-06-06
  • 打赏
  • 举报
回复
写的不错。
julyclyde 2002-06-06
  • 打赏
  • 举报
回复
没必要的垃圾
精华区里我那一手可比这个好多了
加载更多回复(3)

28,404

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧