public static void SaveSection(string fileName, string configName, string serverName, bool isIntegrate, string userId, string password, string dbName, string handleTypeName, ICryptConnection cryptObj) { var doc = new XmlDocument(); doc.Load(fileName); var root = doc.SelectSingleNode("configuration/configSections/section[@name='" + configName + "']"); if (root != null) { root.Attributes["type"].Value = handleTypeName; } else { throw new ArgumentException("数据库配置节点不存在 - " + "configuration/configSections/section[@name='" + configName + "']"); } var node = doc.SelectSingleNode("configuration/" + configName); if (node != null) { string connectionFormat = "{0}Data Source={1};Initial Catalog={2};Persist Security Info=True;"; string connString = string.Format(connectionFormat, isIntegrate ? "Integrated Security=SSPI;" : "", serverName, dbName); if (isIntegrate) { userId = ""; password = ""; } else { userId = cryptObj.EncryptConnString(userId); password = cryptObj.EncryptConnString(password); } node.Attributes["UseEncryption"].Value = "true"; node.Attributes["PartialConnectionString"].Value = connString; node.Attributes["User"].Value = userId; node.Attributes["Password"].Value = password; } else { throw new ArgumentException("数据库配置节点不存在 - " + "configuration/" + configName); } doc.Save(fileName); }
public static void SaveFactory(string fileName, XmlNode itsNode, string serverName, bool isIntegrate, string userId, string password, string dbName, string handleTypeName, ICryptConnection cryptObj) { var connStr = CreateMssqlConnectionString(serverName, isIntegrate, userId, password, dbName); var encode = cryptObj.EncryptConnString(connStr); var xmlnsm = new XmlNamespaceManager(itsNode.OwnerDocument.NameTable); xmlnsm.AddNamespace("urn", "urn:nhibernate-configuration-2.2"); var connection = itsNode.SelectSingleNode("urn:property[@name='connection.connection_string']", xmlnsm); if (connection == null) { connection = itsNode.OwnerDocument.CreateElement("property", "waiting delete"); var attr = itsNode.OwnerDocument.CreateAttribute("name"); attr.Value = "connection.connection_string"; connection.InnerText = encode; connection.Attributes.Append(attr); itsNode.PrependChild(connection); } else { connection.InnerText = encode; } var provider = itsNode.SelectSingleNode("urn:property[@name='connection.provider']", xmlnsm); if (provider == null) { provider = itsNode.OwnerDocument.CreateElement("property", "waiting delete"); var attr = itsNode.OwnerDocument.CreateAttribute("name"); attr.Value = "connection.provider"; provider.InnerText = handleTypeName; provider.Attributes.Append(attr); itsNode.PrependChild(provider); } else { provider.InnerText = handleTypeName; } itsNode.OwnerDocument.InnerXml = itsNode.OwnerDocument.InnerXml.Replace(" xmlns=\"waiting delete\"", ""); itsNode.OwnerDocument.Save(fileName); }