我試圖改變連接字符串值更改連接字符串值在app.config
的connectionString =「數據源=本地主機;初始目錄=跟蹤;堅持安全信息= FALSE;用戶ID = SA ;密碼= p @ ssw0rd「
從我的用戶界面。任何人都可以幫助更改Windows應用程序中用戶界面的設置嗎?
我試圖改變連接字符串值更改連接字符串值在app.config
的connectionString =「數據源=本地主機;初始目錄=跟蹤;堅持安全信息= FALSE;用戶ID = SA ;密碼= p @ ssw0rd「
從我的用戶界面。任何人都可以幫助更改Windows應用程序中用戶界面的設置嗎?
從原始帖子的評論主題,聽起來像OP需要枚舉數據源並允許用戶選擇一個(也許存儲該首選項)。假設情況如下...
如果可能的話,應該使用集成的Windows安全保護到數據庫的實際連接。這是一個best practice。集成的安全性消除了在任何地方存儲憑據的需求。
如果目標是選擇一個數據源,使用C#列出所有databases within an environment並不困難。
用戶的偏好/選擇應該存儲在除app.config之外的其他地方。如果不涉及憑證,註冊表,獨立存儲或XML文件都是有效的選項。
ASP.NET提供了網站管理工具查看併爲您的ASP.NET網站管理的配置設置。這些配置設置存儲在名爲web.config的xml文件中。
的web.config是用於定義配置設置,如連接字符串,認證,授權等應用程序配置文件,ASP.NET應用程序
該工具提供了易於使用的界面編輯配置設置。但是,當您必須手動進行更改時,此工具才能正常工作。有時候,我們可能需要在運行時更改web.config。例如:請求控制連接字符串的用戶,更改會話超時等等。 爲此,ASP.NET提供配置API以編程方式操作web.config中的配置設置。這些API包含在System.Configuration和System.Web.Configuration命名空間中。事實上,內部網站管理工具使用相同的API來編輯配置設置。 The WebConfigurationManager class System.Web.Configuration命名空間用於創建一個Configuration對象。該對象用於讀取和寫入對web.config文件的更改。 See。
見讓我澄清一下,這是你問的不是,但通過閱讀這一點,你就能夠找到一些線索,來了解建議是什麼,什麼是不。
如果這是一個Windows應用程序,那麼您不需要更改app.config文件以更改活動數據庫連接。我已經寫了關於這個on my blog。以下方法
用戶使用C#來更改連接字符串:
public void SaveConnectionString(DbInfo dbinfo, string path,string appConfigFile)
{
try
{
string configFile = Path.Combine(path, appConfigFile);
var doc = new XmlDocument();
doc.Load(configFile);
XmlNodeList endpoints = doc.GetElementsByTagName("connectionStrings");
foreach (XmlNode item in endpoints)
{
if (item.HasChildNodes)
{
foreach (var c in item.ChildNodes)
{
if (((XmlNode)c).NodeType == XmlNodeType.Element)
{
var adressAttribute = ((XmlNode)c).Attributes["name"];
if (adressAttribute.Value.Contains("YourConStrName"))
{
if (dbinfo.dbType == dataBaseType.Embedded)
{
((XmlNode)c).Attributes["connectionString"].Value = SetupConstants.DbEmbededConnectionString;
((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbEmbededConnectionProvider;
}
else if (dbinfo.dbType == dataBaseType.Microsoft_SQL_Server)
{
if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.SQL_Server_Authentication)
{
// ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, SetupConstants.SqlDbName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";
((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, dbinfo.DatabaseName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";
}
else if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.Windows_Authentication)
{
//((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, SetupConstants.SqlDbName);
((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, dbinfo.DatabaseName);
}
((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbSqlConnectionProvider;
}
}
}
}
}
}
doc.Save(configFile);
string exePath = Path.Combine(path, "EDC.Service.exe");
InstallerHelper.EncryptConnectionString(true, exePath);
}
catch (Exception ex)
{
//TODO://log here exception
Helper.WriteLog(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
添加波紋管類DBINFO
public class DbInfo
{
public DataBaseType dbType { get; set; }
public SqlServerAuthenticationType sqlServerAuthType { get; set; }
public string ConnectionString { get; set; }
public string databaseAdress { get; set; }
public string userId { get; set; }
public string password { get; set; }
public string Port { get; set; }
public string DatabaseName { get; set; }
}
public enum DataBaseType
{
Unknown = 0,
Embedded = 1,
Microsoft_SQL_Server =2,
}
public enum SqlServerAuthenticationType
{
Windows_Authentication = 0 ,
SQL_Server_Authentication =1
}
你想要的應用程序來更新它自己的app.config文件?從應用程序本身重寫應用程序的配置文件不是一個好主意。 – 2012-01-07 05:23:21
感謝您的回覆。 – user735627 2012-01-07 05:24:07
借調。這不是一個好策略。如果您需要創建動態連接字符串,則可能不希望將它們存儲在app.config文件中。加密到文件或註冊表是我推薦的。 – 2012-01-07 05:24:59