设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3990|回复: 5
打印 上一主题 下一主题

如何从udl文件中取出BaseConnectionString信息?

[复制链接]

点击这里给我发消息

1#
发表于 2003-5-3 21:38:00 | 显示全部楼层
<>下面的两个函数:
一、MakeADPConnectionless: 用于断开当前ADP的连接,使 BaseConnectionString 为空,一般用在项目退出的时候。

二、sCreateConnection: 用于使用 udl 文件创建当前 ADP 的数据库连接,即:BaseConnectionString,使当前的ADP连接到 udl 文件指定SQL数据库,一般用进入项目时。

<FONT face="Courier New">Sub MakeADPConnectionless()
<FONT color=#008000>'断开当前ADP的连接
'
</FONT>
    Application.CurrentProject.CloseConnection  '关闭连接
    Application.CurrentProject.OpenConnection   '将连接设置为无

End Sub

Public Function sCreateConnection( Byval udlFileName As String) As String
<FONT color=#008000>'********************************************************************
'该函数在ADP中检查连接,如果没有,它将通过输入参数创建一个连接
'
'输入:
' udlFileName: 通用数据连接文件名
'输出:
' 连接状态
'
'********************************************************************
</FONT>
On Error GoTo sCreateConnectionTrap:

    Dim sConnectionString As String

    If Application.CurrentProject.BaseConnectionString = "" Then
    <FONT color=#008000>'表示ADP处于无连接状态</FONT>
   
        sConnectionString = "File Name=" &amp; _
                        CurrentProject.Path &amp; udlFileName
        
        Application.CurrentProject.OpenConnection sConnectionString
        sCreateConnection = "创建了使用 udl 文件 (" &amp; _
                      udlFileName &amp; ")连接到数据库的连接!"
   
    Else <FONT color=#008000>'连接已存在</FONT>
        
        sCreateConnection = "已经存在数据库的连接!"
   
    End If


sCreateConnectionExit:
    Exit Function

sCreateConnectionTrap:
    sCreateConnection = Err.Description
    Resume sCreateConnectionExit

End Function
</FONT></P>
[此贴子已经被作者于2005-1-28 22:09:35编辑过]

点击这里给我发消息

2#
发表于 2003-5-7 21:41:00 | 显示全部楼层
<DIV style="FONT-SIZE: 14px; COLOR: #138; LINE-HEIGHT: 150%"><FONT face=Verdana><FONT color=#000000><b>上一贴有错!</b>

下面的代码在Win2000+SQL2000+Access2002下修改后并测试通过:
</FONT>
<FONT face=Courier>Sub MakeAdpConnectionless()
</FONT><FONT face=Courier><FONT color=#006600>'断开当前adp的连接
'
</FONT>
    Application.CurrentProject.CloseConnection  <FONT color=#006000>'关闭连接</FONT>
    Application.CurrentProject.OpenConnection   </FONT><FONT face=Courier><FONT color=#006000>'将连接设置为无
</FONT>
End Sub

Public Function sCreateConnection(ByVal UDLFileName As String) As String
</FONT><FONT face=Courier><FONT color=#006000>'********************************************************************
'该函数在adp中检查连接,如果没有,它将通过输入参数创建一个连接
'
'输入:
' udlfilename: 通用数据连接文件名
'输出:
' 连接状态
'
'********************************************************************

</FONT>On Error GoTo sCreateConnectionTrap:

    Dim sConnectionString As String

    If Application.CurrentProject.BaseConnectionString = "" Then
    </FONT><FONT face=Courier><FONT color=#006000>'表示adp处于无连接状态
</FONT>   
        sConnectionString = GetConnectionStringFromUDL(CurrentProject.Path &amp; "\" &amp; UDLFileName)
        
        Application.CurrentProject.OpenConnection sConnectionString
        sCreateConnection = "创建了使用 udl 文件 (" &amp; UDLFileName &amp; ")连接到数据库的连接!"
   
    Else <FONT color=#006000>'连接已存在</FONT>
        
        sCreateConnection = "已经存在数据库的连接!"
   
    End If


sCreateConnectionExit:
    Exit Function

sCreateConnectionTrap:
    sCreateConnection = Err.Description
    Resume sCreateConnectionExit

End Function


Public Function GetConnectionStringFromUDL(ByVal UDLFileName As String) As String
    Dim sLine As String
    Dim FileNo As Integer
    If Len(Trim(Dir(UDLFileName &amp; ""))) &gt; 0 Then
        FileNo = FreeFile
        Open UDLFileName For Input As #FileNo
        Do While Left(LCase(sLine), 8) &lt;&gt; "provider" And Not EOF(FileNo)
            Line Input #FileNo, sLine
        Loop
        If Left(LCase(sLine), 8) &lt;&gt; "provider" Then
            GetConnectionStringFromUDL = "0"
        Else
            GetConnectionStringFromUDL = sLine
        End If
        Close #FileNo
    Else
        GetConnectionStringFromUDL = "0"
    End If
End Function
</FONT></FONT></DIV>
[此贴子已经被作者于2005-10-21 11:08:08编辑过]

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-15 10:27 , Processed in 0.084064 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表