0

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

我看过这样的文章,但它们是用 C# 编写的。像这样: http: //www.codeproject.com/Tips/446121/Adding-connection-string-during-installation

我也尝试将代码翻译成 vb.net,但有些错误我无法修复。

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

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

4

1 回答 1

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")
于 2013-07-29T07:33:09.690 回答