예제 #1
0
        /// <summary>
        /// Add a connectionstring to the configuration file
        /// </summary>
        /// <param name="connectionString"></param>
        public static void AddConnectionString(ConnectionStringStruct connectionString)
        {
            // Get the document
            XmlDocument document = GetXmlDocument(GetConfigFile());

            // Get the node wich contains all the connection strings
            XmlNode parentNode = document.SelectSingleNode("configuration/connectionStrings");

            // Create a new element
            XmlElement newElement = document.CreateElement("add");

            // Define the new attributes
            XmlAttribute nameAttribute = document.CreateAttribute("name");

            nameAttribute.Value = connectionString.name;
            XmlAttribute connectionStringAttribute = document.CreateAttribute("connectionString");

            connectionStringAttribute.Value = connectionString.connectionString;
            XmlAttribute providerNameAttribute = document.CreateAttribute("providerName");

            providerNameAttribute.Value = connectionString.providerName;

            // Append them to the new element
            newElement.Attributes.Append(nameAttribute);
            newElement.Attributes.Append(connectionStringAttribute);
            newElement.Attributes.Append(providerNameAttribute);

            // Append the new element to the peren node
            parentNode?.AppendChild(newElement);

            // Save the document
            document.Save(GetConfigFile());
        }
        /// <summary>
        /// Edits the XML configuration file.
        /// </summary>
        /// <param name="dbName">Name of the database.</param>
        /// <param name="providerName">Name of the provider.</param>
        /// <param name="connectionString">The connection string.</param>
        private void EditXmlConfigFile(string dbName, string providerName, string connectionString)
        {
            ConnectionStringStruct connStruct = Settings.GetConnectionString(dbName);

            if (connStruct.Equals(new ConnectionStringStruct()))
            {
                connStruct.name             = dbName;
                connStruct.connectionString = connectionString;
                connStruct.providerName     = providerName;

                Settings.AddConnectionString(connStruct);
            }
            else
            {
                connStruct.connectionString = connectionString;
                connStruct.providerName     = providerName;

                Settings.RemoveConnectionString(dbName);
                Settings.AddConnectionString(connStruct);
            }

            // Close the configuration form
            Close();
        }