zoukankan      html  css  js  c++  java
  • 通用数据链接文件 (*.UDL) 的创建

    '引用 Microsoft OLE DB Service Component 1.0 Type Library
    Option Explicit
    Private Sub Command1_Click()
     Dim x As New MSDASC.DataLinks
     x.hWnd = Me.hWnd
     Dim s As String
     On Error GoTo ErrorHandler
     s = x.PromptNew
     On Error GoTo 0
     If VBA.Len(VBA.Trim(s & "")) > 0 Then
      Dim CommonDialog1 As New MSComDlg.CommonDialog
      CommonDialog1.DefaultExt = ".udl"
      CommonDialog1.Filter = "通用数据链接文件 (*.UDL)|*.udl"
      CommonDialog1.DialogTitle = "保存为通用数据链接文件"
      CommonDialog1.Flags = cdlOFNOverwritePrompt
      CommonDialog1.CancelError = True
      On Error GoTo ErrorHandler
      CommonDialog1.ShowSave
      On Error GoTo 0
      s = "[oledb]" & vbCrLf _
       & "; Everything after this line is an OLE DB initstring" & vbCrLf _
       & s & vbCrLf
      Dim BytesBuffer() As Byte
      BytesBuffer = VBA.StrConv(VBA.StrConv(s, vbUnicode), vbFromUnicode)
      Dim i As Long
      ReDim BytesBuffer0(1) As Byte
      BytesBuffer0(0) = 255 '&HFF
      BytesBuffer0(1) = 254 '&HFE
      If VBA.Len(VBA.Trim(VBA.Dir(CommonDialog1.FileName))) > 0 Then
       VBA.Kill CommonDialog1.FileName
      End If
      On Error GoTo ErrorHandler
      i = VBA.FreeFile
      Open CommonDialog1.FileName For Binary Access Write As #i
      Put #i, , BytesBuffer0
      Put #i, , BytesBuffer
      Close #i
      On Error GoTo 0
      If VBA.MsgBox("Test?", vbYesNo) = vbYes Then
       Dim adoConnection As New ADODB.Connection
       adoConnection.Open "File Name=" & CommonDialog1.FileName
       VBA.MsgBox "OK!"
      End If
     End If
     Exit Sub
    ErrorHandler:
     If Err.Number <> 91 And Err.Number <> 32755 Then
      VBA.MsgBox Err.Number & ":" & vbCrLf & Err.Description
     End If
    End Sub

    Private Sub Command2_Click()
     Dim CommonDialog1 As New MSComDlg.CommonDialog
     CommonDialog1.DefaultExt = ".udl"
     CommonDialog1.Filter = "通用数据链接文件 (*.UDL)|*.udl"
     CommonDialog1.DialogTitle = "打开通用数据链接文件"
     'CommonDialog1.Flags = cdlOFNOverwritePrompt
     CommonDialog1.CancelError = True
     On Error GoTo ErrorHandler
     CommonDialog1.ShowOpen
     On Error GoTo 0
     If VBA.Len(VBA.Trim(VBA.Dir(CommonDialog1.FileName))) > 0 Then
      VBA.MsgBox GetConnectionStringFromUDL(CommonDialog1.FileName)
     End If
     Exit Sub
    ErrorHandler:
     If Err.Number <> 91 And Err.Number <> 32755 Then
      VBA.MsgBox Err.Number & ":" & vbCrLf & Err.Description
     End If
    End Sub

    Public Function GetConnectionStringFromUDL(UDLFileName As String) As String
     If VBA.Len(VBA.Trim(VBA.Dir(UDLFileName & ""))) > 0 Then
      Dim BytesBuffer() As Byte
      ReDim BytesBuffer(VBA.FileLen(UDLFileName) - 133) As Byte
      Dim i As Long
      i = VBA.FreeFile
      Open UDLFileName For Binary Access Read As #i
      Get #i, 129, BytesBuffer
      Close #i
      GetConnectionStringFromUDL = VBA.Trim(VBA.StrConv(VBA.StrConv(BytesBuffer, vbFromUnicode), vbUnicode))
     End If
    End Function

  • 相关阅读:
    Sharding-JDBC多数据源动态切换
    U 盘安装 CentOS 7 时出现 No Caching mode page found 问题的解决
    sudo 密码直接添加到命令行以方便实现脚本自动化
    Python3 Windows 虚拟环境的若干问题
    20 张图让你彻底弄懂 HTTPS 原理!
    全网写得最好的分库分表之 Sharding-JDBC 中间件介绍
    以为线程池很简单,结果第一道题就被干趴下了!
    以为线程池很简单,没想到第一问就被干趴下了
    分布式事务,看这篇就够了!
    我是一个线程池
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2485794.html
Copyright © 2011-2022 走看看