Ejemplo n.º 1
0
        /// <summary>
        /// Reading Default Connection String From AppSettings Xml File
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns type="DataSettings"></returns>
        public static DataSettings ReadConnectionStringFromXml(string fileName)
        {
            try
            {
                if (string.IsNullOrEmpty(fileName))
                    throw new ArgumentNullException("fileName");

                DataSettings dataSettings = new DataSettings();

                //creating new XML document
                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.Load(fileName);

                XmlNode dataConnectionStringNode = xmlDoc.SelectSingleNode("/connectionStrings/connectionString/dataConnectionString");
                string dataConnectionString = dataConnectionStringNode.InnerText;

                XmlNode dataProviderNameNode = xmlDoc.SelectSingleNode("/connectionStrings/connectionString/dataProviderName");
                string dataProviderName = dataProviderNameNode.InnerText;

                dataSettings.DataConnectionString = String.IsNullOrEmpty(dataConnectionString) ? null : dataConnectionString;
                dataSettings.DataProvider = String.IsNullOrEmpty(dataProviderName) ? null : dataProviderName;

                return dataSettings;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected virtual string ComposeSettings(DataSettings settings)
        {
            if (settings == null)
                return "";

            return string.Format("DataProvider: {0}{2}DataConnectionString: {1}{2}",
                                 settings.DataProvider,
                                 settings.DataConnectionString,
                                 Environment.NewLine
                );
        }
        public virtual void SaveSettings(DataSettings settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");

            //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests
            string filePath = LocalSettings.AppSettingsFilePath;
            if (!File.Exists(filePath))
            {
                using (File.Create(filePath))
                {
                    //we use 'using' to close the file after it's created
                }
            }

            var text = ComposeSettings(settings);
            File.WriteAllText(filePath, text);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parsing Connection String To DataSettings From Text File
        /// </summary>
        /// <param name="text"></param>
        /// <returns type="DataSettings"></returns>
        public static DataSettings ParseDataSettingsFromText(string text)
        {
            var dataSettings = new DataSettings();
            if (String.IsNullOrEmpty(text))
                return dataSettings;

            //Old way of file reading. This leads to unexpected behavior when a user's FTP program transfers these files as ASCII (\r\n becomes \n).
            //var settings = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var settings = new List<string>();
            using (var reader = new StringReader(text))
            {
                string str;
                while ((str = reader.ReadLine()) != null)
                    settings.Add(str);
            }

            foreach (var setting in settings)
            {
                var separatorIndex = setting.IndexOf(":");
                if (separatorIndex == -1)
                {
                    continue;
                }
                string key = setting.Substring(0, separatorIndex).Trim();
                string value = setting.Substring(separatorIndex + 1).Trim();

                switch (key)
                {
                    case "DataProvider":
                        dataSettings.DataProvider = value;
                        break;
                    case "DataConnectionString":
                        dataSettings.DataConnectionString = value;
                        break;
                    default:
                        dataSettings.RawDataSettings.Add(key, value);
                        break;
                }
            }

            return dataSettings;
        }