数据库连接字符串子功能

时间:2019-01-30 09:58:19

标签: vb.net-2010

我有一个连接字符串功能。 我添加了一个openfile。 我的问题是,每次引用连接时,用户都必须选择数据库。 我没有使用功能的经验,目前也没有使用它的经验。

下面是代码来自的引用:

有没有一种方法可以设置连接字符串,以便您不必在每次引用连接时都选择数据库?

参考:vb allow user to select sql database file and change connection string(Dataset)

          Public Shared Function cnstr() As String


            Dim openFileDialog1 As New OpenFileDialog()
                openFileDialog1.Title = "Select Database File"
                openFileDialog1.Filter = "MS-Access Files|*.accdb"
                openFileDialog1.FileName = "PhoneBook"
                openFileDialog1.InitialDirectory = "C:\Temp"  'Suggested path for where the file could exist
                openFileDialog1.ShowDialog()

            If openFileDialog1.FileName = "" Then
                Application.Exit()  'Decide what to do when no file is selected

            End If

            Dim dbNameWithPath As String = openFileDialog1.FileName 
           'openFileDialog1.FileName is where the selected file is stored


                Return "Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & dbNameWithPath

            'Original Connection String  Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data\" & PhoneBook & "\PhoneBook.accdb;Jet OLEDB:Database Password="

        End Function

0 个答案:

没有答案