在安装vb.net项目期间添加连接字符串

时间:2013-03-28 13:20:57

标签: vb.net visual-studio-2010 connection connection-string

我想创建一个安装程序,其中用户指定数据库的连接字符串的位置。

我见过这样的文章,但他们在C#中。 像这样:http://www.codeproject.com/Tips/446121/Adding-connection-string-during-installation

我也尝试将代码翻译成vb.net,但是有一些我无法解决的错误。

如果有人能告诉我该怎么做,或任何可能对我有帮助的文章真的是一个很好的帮助。

我想在我的vb.net项目中执行此操作,而不是在C#

1 个答案:

答案 0 :(得分:0)

以下是转换为VB .NET的代码

'Installer.vb
Dim dataSource As String = "Data Source=" & Context.Parameters("DataSource")
Dim initialcatalog As String = "Initial Catalog=" & Context.Parameters("InitialCatalog")
Dim user As String = "User ID=" & Context.Parameters("UserID")
Dim password As String = "Password=" & Context.Parameters("Password")
dataSource = dataSource & ";" & initialcatalog & ";" & user & ";" & password
dataSource = dataSource
MessageBox.Show("instance=" & dataSource)

Dim map As New ExeConfigurationFileMap()
MessageBox.Show(System.Reflection.Assembly.GetExecutingAssembly().Location & ".config")

'Getting the path location 
Dim configFile As String = String.Concat(System.Reflection.Assembly.GetExecutingAssembly().Location, ".config")
map.ExeConfigFilename = configFile

Dim config As System.Configuration.Configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)
Dim connectionsection As String = config.ConnectionStrings.ConnectionStrings("MyConnectionString").ConnectionString
Dim connectionstring As ConnectionStringSettings = Nothing

If connectionsection IsNot Nothing Then
    config.ConnectionStrings.ConnectionStrings.Remove("MyConnectionString")
   'MessageBox.Show("removing existing Connection String")
End If

connectionstring = New ConnectionStringSettings("MyConnectionString", dataSource)
config.ConnectionStrings.ConnectionStrings.Add(connectionstring)
config.Save(ConfigurationSaveMode.Modified, True)
ConfigurationManager.RefreshSection("connectionStrings")