簡體   English   中英

vb.net 如果找不到數據庫打開 OpenFileDialog

[英]vb.net if database not found open a OpenFileDialog

我使用以下代碼設置與我的數據庫的連接: Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")我想擴展這個 function 通過檢查數據庫是否存在,如果不打開FileOpenDialog來選擇另一個文件夾中的另一個數據庫。 我似乎無法讓它工作,因為當我將FileOpenDialog放在我的主窗體上時,它會引發異常錯誤。

Public Function Jokendb() As OleDbConnection
Dim FileName As String = Application.StartupPath & "\FileRename v5.4.accdb"
            If IO.File.Exists(FileName) Then
                Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")
            Else
                'Dim result As DialogResult = OpenFileDialog1.ShowDialog()
                Dim str As String = OpenFileDialog1.FileName.ToString()
                'If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                ' Get the file name.
                Dim path As String = OpenFileDialog1.FileName
                Try
                    ' Read in text.
                    Dim text As String = File.ReadAllText(path)
                    Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & text)
                Catch ex As Exception
    
                    ' Report an error.
                    Me.Text = "Error"
                End Try
                'End If
            End If
    End Function

我不知道為什么要評論這些行,但我認為您的代碼可以與這些更改一起使用:

    Public Function Jokendb() As OleDbConnection
Dim FileName As String = Application.StartupPath & "\FileRename v5.4.accdb"
            If IO.File.Exists(FileName) Then
                Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")
            Else
               OpenFileDialog1.ShowDialog()
                'Dim str As String = OpenFileDialog1.FileName.ToString()
                'If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                ' Get the file name.

                 Dim path As String = ""
                    If OpenFileDialog1.filename <> "" Then
                        path = OpenFileDialog1.FileName
                    End If
                Try
                    ' Read in text.
                    Dim text As String = File.ReadAllText(path)
                    Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & text)
                Catch ex As Exception
    
                    ' Report an error.
                    Me.Text = "Error"
                End Try
                'End If
            End If
    End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM