【问题标题】:Save a new connection string from within a windows forms app?从 Windows 窗体应用程序中保存新的连接字符串?
【发布时间】:2011-08-26 00:36:52
【问题描述】:

我正在编写一个 Windows 窗体应用程序,VS2010,NET Framework 4.0,用 VB 编码。我在我的应用程序中使用 Microsoft 数据连接配置对话框来选择数据源。它似乎工作正常并正确创建了 ConnectionString。但是,新的连接字符串不会保存到 app.config 文件中。我没有编写代码来保存文件,我在这方面盲目地徘徊。这是我正在使用的代码:

  Public Sub SaveConnectionString(ByVal strConnectionName As String, strConnectionString As String)

    Dim Config As Configuration
    Dim Section As ConnectionStringsSection
    Dim Setting As ConnectionStringSettings
    Dim ConnectionFullName As String

    'There is no inbuilt way to change application 
    'setting values in the config file.
    'So that needs to be done manually by calling config section object.

    Try
        'Concatenate the full settings name
        'This differs from Jakob Lithner. Runtime Connection Wizard
        'The ConnectionFullName needs to 
        'refer to the Assembly calling this DLL

        ConnectionFullName = String.Format("{0}.MySettings.{1}", _
            System.Reflection.Assembly.GetCallingAssembly.EntryPoint.DeclaringType.Namespace, strConnectionName)

        'Point out the objects to manipulate
        Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Section = CType(Config.GetSection("connectionStrings"),  _
            ConnectionStringsSection)
        Setting = Section.ConnectionStrings(ConnectionFullName)

        MsgBox("To File: " & Config.FilePath.ToString & vbCrLf &
               "Conn. String: " & strConnectionString & vbCrLf &
               "Conn. Name: " & strConnectionName,
               MsgBoxStyle.Information, "Saving Connection String...")

        'Ensure connection setting is defined 
        '(Note: A default value must be set to save the connection setting!)
        If IsNothing(Setting) Then Throw New Exception("There is no connection with this name defined in the config file.")

        'Set value and save it to the config file
        'This differs from Jakob Lithner. Runtime Connection Wizard
        'We only want to save the modified portion of the config file 
        Setting.ConnectionString = strConnectionString
        Config.Save(ConfigurationSaveMode.Full, True)

    Catch ex As Exception

    End Try
End Sub

MsgBox 显示 .config 文件的预期文件名和路径,但该文件从未更新。我试过从VS2010 IDE运行这个,直接运行编译创建的.exe文件,创建一个安装程序并安装程序并运行它(程序本身运行良好),并安装在另一台机器上。始终相同 - 没有错误消息并且 .config 文件没有更新。

程序正在从 appname.exe.config 文件中读取连接字符串。如果我手动编辑 exe.config 文件,我可以毫无问题地使用不同的连接字符串。

如果有人可以提供任何指导,我将非常感激!提前致谢。

【问题讨论】:

标签: winforms connection-string configuration-files


【解决方案1】:

我的问题获得了相当多的页面浏览量,但没有人冒险回答。幸运的是,我找到了自己的答案。感谢 http://www.thecodemonk.com/2008/02/18/tableadapter-connection-strings 提供这个简单的解决方案。

甚至不要尝试保存到应用程序设置“AppNameConnectionString”。相反,只需将所需的连接字符串保存为用户设置,即字符串类型的“ActiveConnectionString”。我用这个替换了我在原始问题中引用的整个代码块

       My.Settings.ActiveConnectionString = strConn ' previously built conn string
       My.Settings.Save()

允许此用户设置成为活动连接字符串的是与管理设置的 .Net 关联的事件。在项目设置上有一个“查看代码”按钮可以访问设置的事件处理程序。在 SettingsLoaded 事件中,只需将应用连接字符串设置设置为您的用户连接字符串设置:

       Private Sub MySettings_SettingsLoaded(sender As Object, e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
        Me.Item("MyAppNameConnectionString") = Me.Item("ActiveConnectionString")
    End Sub

这就是您的用户设置在应用加载时成为工作连接字符串所需的全部内容。

在设置代码中添加另一个事件处理程序,当您从应用程序中保存新的连接字符串时,它将立即激活!

    Private Sub MySettings_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
        If e.PropertyName = "ActiveConnectionString" Then
            Me.Item("MyAppNameConnectionString") = Me.Item("ActiveConnectionString")
        End If
    End Sub

再次感谢 TheCodeMonk 分享这个极其简单但显然非常难以捉摸的解决方案!

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多