问题
I want to create an installer wherein the user specifies where the connection string of the database.
I've seen a articles like this but they're in C#. like this one: http://www.codeproject.com/Tips/446121/Adding-connection-string-during-installation
I also tried to translate the code into vb.net but there are some errors I can't fix.
If anyone can tell me how to do it or any article that might help me is really a great help.
I want to do this in my vb.net project not in C#
回答1:
Here is the code converted to 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")
来源:https://stackoverflow.com/questions/15682988/adding-connection-string-during-installation-of-vb-net-project