StringBuilder sb2 = new StringBuilder();
sb2.Append("data source=");
sb2.Append(temp1);
sb2.Append(";");
sb2.Append("initial catalog=");
sb2.Append(temp2);
sb2.Append(";persist security info=False;");
sb2.Append("user id=");
sb2.Append(temp3);
sb2.Append(";");
sb2.Append("pwd=");
sb2.Append(temp4);
sb2.Append(";packet size=4096");

StringBuilder xmlpath = new StringBuilder();
xmlpath.Append(filepath);
xmlpath.Append("\\");
xmlpath.Append("Web.config"); XmlDocument doc = new XmlDocument();
doc.Load(xmlpath.ToString());   //加载Web.Config
XmlElement elem=doc.DocumentElement; XmlElement newelem = doc.CreateElement ("appSettings");
XmlElement elem1 = doc.CreateElement("add"); XmlNodeList nodes = doc.SelectNodes("/configuration/appSettings/add"); //连接字符串赋值                         foreach(XmlNode node in nodes)
{
switch (node.Attributes["key"].Value)
{
case "strConnection":
node.Attributes["value"].Value = sb2.ToString();
break;
default:
// Don't do anything
break; 
}
}
doc.Save(xmlpath.ToString());   //保存修改