我如何在运行时在vb.net中使用连接字符串更新app.config文件 [英] how can i update app.config file with connection string at run time in vb.net

查看:216
本文介绍了我如何在运行时在vb.net中使用连接字符串更新app.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,我希望用户在运行该应用程序之前通过提供数据源,数据库的用户ID和密码来决定他们要在运行时连接到哪个数据库.我正在使用vb.net和oracle.any的输入将不胜感激.谢谢.下面的代码是我所做的,但似乎并不能使我获得想要的结果.

i have this application and i want user to decide which database they want to connect to at run time by supplying the data source,user id and password of the database before the application will run. i am using vb.net with oracle.any input will be appreciated. thanks. The following codes is what I have done but it does not seem to get me the result I want.

Public Sub savenewconnection(ByVal id As String, ByVal pass As String, ByVal source As String)
       Try
           Dim newdbcon As String
           Dim oldconnection As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("mydbcon")
           Dim dbcon As String = oldconnection.ConnectionString
           Dim builder As New OleDbConnectionStringBuilder(dbcon)
           builder.Add("Data Source", source)
           builder.Add("Password", pass)
           builder.Add("User ID", id)
           newdbcon = builder.ConnectionString
           Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
           config.ConnectionStrings.ConnectionStrings("mydbcon").ConnectionString = newdbcon
           config.Save(ConfigurationSaveMode.Modified)
           ConfigurationManager.RefreshSection("ConnectionStrings")

       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
   End Sub

推荐答案

在运行时无法更改应用程序配置文件中的连接字符串.完全是因为这是应用程序设置而不是用户设置.
您只需要使用一个真实的字符串并将其存储为用户设置即可.
It is not possible to change the connection strings in your application config file at runtime. Quite simply because this is an application setting and not a user setting.
What you could do is simply use a real string and store that as a user setting.


这篇关于我如何在运行时在vb.net中使用连接字符串更新app.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆