【问题标题】:Pass Dynamic connection string to another Windows Form (VB.NET)将动态连接字符串传递给另一个 Windows 窗体 (VB.NET)
【发布时间】:2012-02-20 15:15:48
【问题描述】:

我正在创建一个 Windows 应用程序,它在应用程序启动时需要动态连接字符串(用户需要通过表单提供 db 凭据),输入连接凭据后,用户重定向到新的 win 表单 一切正常,但我怎样才能将我的动态连接传递给另一个表单。

我试图将它保存到 App 变量但我不能(我认为它是只读的) 我也尝试将其保存到注册表但无法检索值。

还有其他选择吗?比如将 ConString 写入和检索到文本文件或 XML

谢谢

【问题讨论】:

  • 只有一个连接字符串。所以让它共享是一种简单的方法。

标签: vb.net winforms connection-string


【解决方案1】:

这是获取和设置注册表值的方法:

Public Function GetRegistryValue(ByVal KeyName As String, Optional ByVal DefaultValue As Object = Nothing) As Object
        Dim res As Object = Nothing
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
            If k IsNot Nothing Then
                res = k.GetValue(KeyName, DefaultValue)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
        Return res
End Function

Public Sub SetRegistryValue(ByVal KeyName As String, ByVal _Value As Object)
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
            If k IsNot Nothing Then
                k.SetValue(KeyName, _Value)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
                k.SetValue(KeyName, _Value)
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
End Sub

100% 有效,请将此答案标记为正确

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多