首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

在程序运行时怎样修改App.config文件的数据库配置节?

2011-12-13 
在程序运行时怎样修改App.config文件的数据库配置节??????????????????急....在线等connectionStringsa

在程序运行时怎样修改App.config文件的数据库配置节??????????????????急....在线等
<connectionStrings>
                <add   name= "conString "   connectionString= "Database=newsinco;Server=192.168.0.66;uid=sa;pwd=aaa;Connection   Reset=FALSE "
                        providerName= "System.Data.SqlClient "   />
        </connectionStrings>
我知道是用下面的那个类,但具体不知道怎么用?求高手帮忙,真的很急,今天下午老大要我搞定,晕,具体怎要显示以前的,和怎样去修改,
                        Configuration   config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


[解决办法]



(前台页面代码)
<%@ Page language= "c# " Codebehind= "WebForm1.aspx.cs " AutoEventWireup= "false " Inherits= "WebApplication1.WebForm1 " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio 7.0 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript (ECMAScript) ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id= "Form1 " method= "post " runat= "server ">
<FONT face= "宋体 ">
<asp:TextBox id= "TextBox1 " style= "Z-INDEX: 101; LEFT: 164px; POSITION: absolute; TOP: 190px " runat= "server " Width= "204px " Height= "25px "> </asp:TextBox>
<asp:Button id= "Button1 " style= "Z-INDEX: 102; LEFT: 386px; POSITION: absolute; TOP: 188px " runat= "server " Width= "78px " Height= "25px " Text= "Button "> </asp:Button>
<asp:DropDownList id= "DropDownList1 " style= "Z-INDEX: 103; LEFT: 29px; POSITION: absolute; TOP: 192px " runat= "server " Width= "130px " Height= "22px "> </asp:DropDownList>
</FONT>
</form>
</body>
</HTML>

(后台CS代码)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml ;


namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;

public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//打开某文件(假设WEB。CONFIG在根目录中)
string filename=Server.MapPath( "/ ") + @ "\web.config ";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);

XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()== "appsettings ")


{
XmlNodeList _node=element.ChildNodes;
if ( _node.Count > 0 )
{
DropDownList1.Items.Clear();
foreach(XmlElement el in _node)
{
DropDownList1.Items.Add(el.Attributes[ "key "].InnerXml);
}
}
}
}
}
}

private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}

#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string filename=Server.MapPath( "/ ") + @ "\web.config ";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);

XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()== "appsettings ")
{
XmlNodeList _node=element.ChildNodes;
if ( _node.Count > 0 )
{
foreach(XmlElement el in _node)
{
if(el.Attributes[ "key "].InnerXml.ToLower()==this.DropDownList1.SelectedItem.Value.ToLower())
{
el.Attributes[ "value "].Value=this.TextBox1.Text;
}
}
}
}
}
xmldoc.Save(filename);
}
}
}

[解决办法]
修改可以直接对其进行Xml的相关操作即可..

不也是一个Xml文件吗?
[解决办法]
就用lizhizhe2000(彬彬) 的方法
Configuration类只提供了读取配置信息的方法,不能修改.config文件的
[解决办法]
错误1类型“System.Configuration.Internal.IConfigErrorInfo”在未被引用的程序集中定义。必须添加对程序集“System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用。F:\MyWeb\DBToCode\DBToCode\Code\DBConfig.cs3313DBToCode

[解决办法]
读出来:
private void config_bind()
{
string str_webconfig = HttpContext.Current.Server.MapPath(Request.ApplicationPath + "/ ")+ "web.config ";
DataSet dsxml = new DataSet();
dsxml.ReadXml(str_webconfig);
sitename.Text = dsxml.Tables[1].Rows[0][1].ToString();
sitelink.Text = dsxml.Tables[1].Rows[1][1].ToString();
logo.Text = dsxml.Tables[1].Rows[2][1].ToString();
banner.Text = dsxml.Tables[1].Rows[3][1].ToString();
title.Text = dsxml.Tables[1].Rows[4][1].ToString();
thename.Text = dsxml.Tables[1].Rows[5][1].ToString();
themail.Text = dsxml.Tables[1].Rows[6][1].ToString();
theright.Text = dsxml.Tables[1].Rows[7][1].ToString();
keywords.Text = dsxml.Tables[1].Rows[8][1].ToString();
style.Text = dsxml.Tables[1].Rows[9][1].ToString();
mdbpath.Text = dsxml.Tables[1].Rows[10][1].ToString();

}
-----------------------------------------------
写进去:
private void updatebtn_Click(object sender, System.EventArgs e)
{
string str_webconfig =HttpContext.Current.Server.MapPath(Request.ApplicationPath + "/ ")+ "web.config ";
DataSet dsxml = new DataSet();
try
{
dsxml.ReadXml(str_webconfig);
dsxml.Tables[1].Rows[0][1] = sitename.Text;
dsxml.Tables[1].Rows[1][1] = sitelink.Text;


dsxml.Tables[1].Rows[2][1] = logo.Text;
dsxml.Tables[1].Rows[3][1] = banner.Text;
dsxml.Tables[1].Rows[4][1] = title.Text;
dsxml.Tables[1].Rows[5][1] = thename.Text;
dsxml.Tables[1].Rows[6][1] = themail.Text;
dsxml.Tables[1].Rows[7][1] = theright.Text;
dsxml.Tables[1].Rows[8][1] = keywords.Text;
dsxml.Tables[1].Rows[9][1] = DropDownList1.SelectedItem.Value;
dsxml.Tables[1].Rows[10][1] = mdbpath.Text;
dsxml.AcceptChanges();
dsxml.WriteXml(str_webconfig);
dsxml.Clear();
Message.Text = " <p align=center> <font color=red> 系统设置修改成功!! </font> </p> ";
}
catch (Exception exc)
{
Message.Text = " <p align=center> <font color=red> 写入文件发生错误!! </font> <br> 错误如下: </p> " + exc.ToString();
}
}
[解决办法]
<globalization requestEncoding= "gb2312 " responseEncoding= "gb2312 " />

</system.web>
<appSettings>
<add key= "Con " value= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=填你数据库的连接字符串 " />
</appSettings>
</configuration>
[解决办法]
如果是自订的App.config就用利用XmlDocument去Load你的App.config,然后再用XmlDocument.SelectSinglNode(xPath)得到你的XmlNode,再用XmlNode.InnerText去取你的值,如果是Attribute就用的XmlNode[ "Attribute "].Value得到.

热点排行