想要在Windows應用程序中利用asp.net的成員資格提供程序以編程方式更改數據庫的connecton字符串。 system.configuration命名空間允許更改用戶設置,但是,我們希望調整應用程序設置?是否需要編寫一個利用XML來修改類的類?是否需要刪除當前連接(可以選擇一個連接來清除)並添加一個新連接?可以調整現有的連接字符串嗎?VS2005 C#以編程方式更改app.config中包含的連接字符串
9
A
回答
6
您可以用編程方式使用System.Configuration命名空間打開配置:
myConfig.ConnectionStrings.ConnectionStrings
可以修改:
Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
然後你就可以在訪問連接字符串集合然後收集您想要的數據,然後在配置對象上調用.Save()
。
0
使用ConnectionStringsSection類。該文檔甚至提供了一個關於如何創建一個新的ConnectionString的例子,並讓框架將它保存到配置文件中,而不必實現整個XML shebang。
請參閱here並向下瀏覽以查看示例。
8
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create a connection string element and
// save it to the configuration file.
// Create a connection string element.
ConnectionStringSettings csSettings =
new ConnectionStringSettings("My Connection",
"LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
"Initial Catalog=aspnetdb", "System.Data.SqlClient");
// Get the connection strings section.
ConnectionStringsSection csSection =
config.ConnectionStrings;
// Add the new element.
csSection.ConnectionStrings.Add(csSettings);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
9
必須做到這一點確切的事情。這是爲我工作的代碼:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["Blah"].ConnectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
相關問題
- 1. 如何在vs2005中以編程方式指定dataset.xsd中的連接字符串
- 2. 更改連接字符串值在app.config
- 3. 以編程方式更改使用c的mysql連接字符串#
- 4. 如何以編程方式更改連接字符串un LINQ C#Winforms
- 5. App.Config中連接字符串
- 6. 如何在c#中的app.config中編寫mysql連接字符串?
- 7. 更改包含鏈接的字符串
- 8. 哪裏存儲連接字符串?並以編程方式更改它
- 9. 使用MVC 3以編程方式更改或添加連接字符串
- 10. 永久更改app.config中的連接字符串從代碼
- 11. 如何更改app.config中的連接字符串
- 12. 在運行時更改App.config中的連接字符串
- 13. Clickonce app.config連接字符串
- 14. C#使用appSettings參數在app.config中編寫連接字符串
- 15. dll的app.config中的連接字符串
- 16. 解密連接字符串中的App.config
- 17. app.config中的連接字符串
- 18. C++修改字符串以包含字符串repr。的整數
- 19. 以編程方式更改字符串的語言
- 20. 更改連接字符串在C#.NET
- 21. 使用InstallShield 2011安裝程序更改app.config中的連接字符串
- 22. Objective-C:以編程方式更改@property(不包含預先構建的方法)
- 23. 在app.config中加密連接字符串
- 24. Android - 以編程方式更改字符串本地
- 25. 以編程方式更改字符串資源在android
- 26. 更改連接字符串
- 27. 更改連接字符串
- 28. 更改連接字符串
- 29. 如何以編程方式更改「包含」佈局的邊距
- 30. 以編程方式將包含在字符串中的JSP轉換爲Servlet
這是添加連接字符串,而不是修改現有的 – Thomas 2012-02-08 09:32:57