Esempio n. 1
0
 public bool SaveConnConfig(string fileName, string serverName, bool isIntegrate, string userId, string password, string dbName, string handleTypeName, ICryptConnection cryptObj, out string errMsg)
 {
     try
     {
         if (!IsSuccess)
         {
             errMsg = ErrMessage;
             return(false);
         }
         if (ItsNode == null)
         {
             errMsg = "未绑定到XML节点";
             return(false);
         }
         if (ConnType.Equals("nhibernate", StringComparison.OrdinalIgnoreCase))
         {
             ConnConfigAnalyzer.SaveFactory(fileName, ItsNode, serverName, isIntegrate, userId, password, dbName, handleTypeName, cryptObj);
         }
         else if (ConnType.Equals("common.database", StringComparison.OrdinalIgnoreCase))
         {
             ConnConfigAnalyzer.SaveSection(fileName, ConnConfigName, serverName, isIntegrate, userId, password, dbName, handleTypeName, cryptObj);
         }
         else
         {
             errMsg = "不支持的数据库连接类型 - " + ConnType;
             return(false);
         }
         errMsg = "";
         return(true);
     }
     catch (Exception ex)
     {
         errMsg = ex.Message;
         return(false);
     }
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }