通用資料連結檔案 (*.UDL) 的建立 (轉)
通用資料連結檔案 (*.UDL) 的建立 (轉)[@more@]引用 OLE 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 MmDlg.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
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 MmDlg.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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992183/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 4243.全文檢索與檔案匯出試驗 ⑤建立“連結到檔案、連結到資料夾”最快的方式是什麼?...
- Qt寫入excel檔案window和Linux通用,支援超連結QTExcelLinux
- 檔案連結
- 【ASM】ASM資料檔案和OS檔案(FILESYSTEM)轉移方法總結ASM
- SpringBoot專案使用yml檔案連結資料庫異常Spring Boot資料庫
- Linux---檔案、軟連結於硬連結檔案Linux
- 資料結構實驗之連結串列六:有序連結串列的建立資料結構
- 織夢CMS(dedecms)的資料庫連線檔案_織夢連線資料庫檔案資料庫
- Qt 選擇資料夾、建立資料夾以及建立檔案QT
- 資料結構實驗之連結串列二:逆序建立連結串列資料結構
- 資料結構之單連結串列的建立與刪除資料結構
- [20190410]dg建立臨時表檔案資料檔案.txt
- hadoop 資料夾檔案的建立與刪除Hadoop
- 利用java建立檔案或者資料夾Java
- 資料結構之連結串列:206. 反轉連結串列資料結構
- 資料結構實驗之連結串列一:順序建立連結串列資料結構
- 必須先建立資料夾再建立檔案嗎
- 連結串列-雙向通用連結串列
- 深入 LevelDB 資料檔案 SSTable 的結構
- 資料結構:單迴圈連結串列的建立插入與刪除資料結構
- 資料結構-malloc申請動態空間-連結串列的建立資料結構
- express入門04 資料庫連線 表結構建立 模型建立Express資料庫模型
- 連結串列-雙向非通用連結串列
- iis連線資料庫的檔案是哪個資料庫
- QGIS如何開啟ArcGIS建立的GDB資料庫檔案資料庫
- dotnet 6 使用 CreateSymbolicLink 建立資料夾符號連結Symbol符號
- 網站連線資料庫配置檔案網站資料庫
- 資料結構--單連結串列的建立和遍歷(程式碼優化)資料結構優化
- FileUtils類建立、刪除檔案及資料夾
- (12)caffe總結之影像資料轉換成db(leveldb/lmdb)檔案
- 根據網路連線(檔案連結)下載檔案到本地
- ubuntu20.04系統右鍵新增建立連結檔案快捷方式的教程Ubuntu
- 單連結串列的建立
- shp檔案轉dwg並建立文字標註
- Java讀取properties檔案連線資料庫Java資料庫
- 1.7.2. 使用ORAPWD建立資料庫密碼檔案資料庫密碼
- 遷移資料庫的檔案到不同路徑(轉)資料庫
- 日誌檔案使用小結(轉)
- Stimulsoft Reports如何建立新的資料轉換、編輯資料轉換