adding connection string during installation of vb.net project

偶尔善良 提交于 2019-12-25 01:50:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!