关于WinForm中设置Appconfig的问题
问题是这样的。1.我需要连接多个数据库,所以我在登录的时候有一个数据库选项。2.我将连接字符写在了appconfig里代码是:<connectionStrings><ad...
问题是这样的。
1.我需要连接多个数据库,所以我在登录的时候有一个数据库选项。
2.我将连接字符写在了appconfig里
代码是:
<connectionStrings>
<add name="connString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后,如何动态修改connectionString的值呢? 求教。。。。。 展开
1.我需要连接多个数据库,所以我在登录的时候有一个数据库选项。
2.我将连接字符写在了appconfig里
代码是:
<connectionStrings>
<add name="connString" connectionString="Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后,如何动态修改connectionString的值呢? 求教。。。。。 展开
2013-05-11
展开全部
webconfig是个xml文件,就用操作xml文件的方法修改节点的值.//修改app.config配置文件中的链接字符串- connectionStrings#region /修改app.config配置文件中的链接字符串-
public void up_AppConfing(string URL, string Values)
{
URL=URL+"bin\\Debug\\";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = URL + "Eson.ETrain.Windows.exe.config";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNode node = doc.SelectSingleNode("configuration/connectionStrings");
for (int i = 0; i < node.ChildNodes.Count; i++)
{
//获得将当前元素的key属性
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (node.ChildNodes[i].Attributes["name"].Value == "ETrainDataAccess")
{
node.ChildNodes[i].Attributes["connectionString"].Value = Values;
}
if (node.ChildNodes[i].Attributes["name"].Value == "UMSDataAccess")
{
node.ChildNodes[i].Attributes["connectionString"].Value = Values;
}
}
//保存上面的修改
doc.Save(strFileName);
}
#endregion
public void up_AppConfing(string URL, string Values)
{
URL=URL+"bin\\Debug\\";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = URL + "Eson.ETrain.Windows.exe.config";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNode node = doc.SelectSingleNode("configuration/connectionStrings");
for (int i = 0; i < node.ChildNodes.Count; i++)
{
//获得将当前元素的key属性
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (node.ChildNodes[i].Attributes["name"].Value == "ETrainDataAccess")
{
node.ChildNodes[i].Attributes["connectionString"].Value = Values;
}
if (node.ChildNodes[i].Attributes["name"].Value == "UMSDataAccess")
{
node.ChildNodes[i].Attributes["connectionString"].Value = Values;
}
}
//保存上面的修改
doc.Save(strFileName);
}
#endregion
展开全部
class AppConfig
{
/// <summary>
/// 导入connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <returns>值</returns>
public String LoadConnectionValue(string Key)
{
ConfigurationManager.RefreshSection("connectionStrings");
string value = "";
value = ConfigurationManager.ConnectionStrings[Key].ToString();
return value;
}
/// <summary>
/// 更新connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <param name="Value">值</param>
public bool UpdateConnectionKey(string Key, string Value)
{
bool Success = false;
ConfigXmlDocument xmlDoc = new ConfigXmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
XmlNode ConnectionNode = xmlDoc.SelectSingleNode("configuration/connectionStrings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in ConnectionNode)
{
if (childNode.NodeType != XmlNodeType.Comment)
{
try
{
if (childNode.Attributes.GetNamedItem("name").Value == Key)
{
childNode.Attributes.GetNamedItem("connectionString").Value = Value;
}
Success = true;
}
catch {}
}
}
if (Success)
{
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
return Success;
}
}
调用方法
AppConfig = new AppConfig();
config.LoadConnectionValue("connString").ToString(); //获得connectionString
config.UpdateConnectionKey("connString",“更改后的值”); //更新值
这个比较全歪
{
/// <summary>
/// 导入connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <returns>值</returns>
public String LoadConnectionValue(string Key)
{
ConfigurationManager.RefreshSection("connectionStrings");
string value = "";
value = ConfigurationManager.ConnectionStrings[Key].ToString();
return value;
}
/// <summary>
/// 更新connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <param name="Value">值</param>
public bool UpdateConnectionKey(string Key, string Value)
{
bool Success = false;
ConfigXmlDocument xmlDoc = new ConfigXmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
XmlNode ConnectionNode = xmlDoc.SelectSingleNode("configuration/connectionStrings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in ConnectionNode)
{
if (childNode.NodeType != XmlNodeType.Comment)
{
try
{
if (childNode.Attributes.GetNamedItem("name").Value == Key)
{
childNode.Attributes.GetNamedItem("connectionString").Value = Value;
}
Success = true;
}
catch {}
}
}
if (Success)
{
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
return Success;
}
}
调用方法
AppConfig = new AppConfig();
config.LoadConnectionValue("connString").ToString(); //获得connectionString
config.UpdateConnectionKey("connString",“更改后的值”); //更新值
这个比较全歪
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-11
展开全部
class AppConfig
{
/// <summary>
/// 导入connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <returns>值</returns>
public String LoadConnectionValue(string Key)
{
ConfigurationManager.RefreshSection("connectionStrings");
string value = "";
value = ConfigurationManager.ConnectionStrings[Key].ToString();
return value;
}
/// <summary>
/// 更新connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <param name="Value">值</param>
public bool UpdateConnectionKey(string Key, string Value)
{
bool Success = false;
ConfigXmlDocument xmlDoc = new ConfigXmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
XmlNode ConnectionNode = xmlDoc.SelectSingleNode("configuration/connectionStrings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in ConnectionNode)
{
if (childNode.NodeType != XmlNodeType.Comment)
{
try
{
if (childNode.Attributes.GetNamedItem("name").Value == Key)
{
childNode.Attributes.GetNamedItem("connectionString").Value = Value;
}
Success = true;
}
catch {}
}
}
if (Success)
{
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
return Success;
}
}
调用方法
AppConfig = new AppConfig();
config.LoadConnectionValue("connString").ToString(); //获得connectionString
config.UpdateConnectionKey("connString",“更改后的值”); //更新值
这个比较全歪
{
/// <summary>
/// 导入connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <returns>值</returns>
public String LoadConnectionValue(string Key)
{
ConfigurationManager.RefreshSection("connectionStrings");
string value = "";
value = ConfigurationManager.ConnectionStrings[Key].ToString();
return value;
}
/// <summary>
/// 更新connectionStrings配置
/// </summary>
/// <param name="Key">索引</param>
/// <param name="Value">值</param>
public bool UpdateConnectionKey(string Key, string Value)
{
bool Success = false;
ConfigXmlDocument xmlDoc = new ConfigXmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
XmlNode ConnectionNode = xmlDoc.SelectSingleNode("configuration/connectionStrings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in ConnectionNode)
{
if (childNode.NodeType != XmlNodeType.Comment)
{
try
{
if (childNode.Attributes.GetNamedItem("name").Value == Key)
{
childNode.Attributes.GetNamedItem("connectionString").Value = Value;
}
Success = true;
}
catch {}
}
}
if (Success)
{
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "ExampleCode.exe.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
return Success;
}
}
调用方法
AppConfig = new AppConfig();
config.LoadConnectionValue("connString").ToString(); //获得connectionString
config.UpdateConnectionKey("connString",“更改后的值”); //更新值
这个比较全歪
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询