示例#1
0
        /// <summary>
        /// Gets data settings from the old txt file
        /// </summary>
        /// <param name="data">Old txt file data</param>
        /// <returns>Data settings</returns>
        protected static DataSettings LoadDataSettingsFromOldFile(string data)
        {
            var dataSettings = new DataSettings();

            using var reader = new StringReader(data);
            string settingsLine;

            while ((settingsLine = reader.ReadLine()) != null)
            {
                var separatorIndex = settingsLine.IndexOf(':');
                if (separatorIndex == -1)
                {
                    continue;
                }

                var key   = settingsLine[0..separatorIndex].Trim();
        public DataTable GetDataTable(string _query)
        {
            DataSettingsManager _dataSettingsManager = new DataSettingsManager();
            DataSettings        _dataSettings        = _dataSettingsManager.LoadSettings();
            SqlConnection       _sqlConnection       = new SqlConnection(_dataSettings.DataConnectionString + ";Connection Timeout=0");
            DataTable           dt = new DataTable();

            if (_sqlConnection.State == ConnectionState.Closed)
            {
                _sqlConnection.Open();
            }
            SqlDataAdapter _da = new SqlDataAdapter(_query, _sqlConnection);

            _da.Fill(dt);
            return(dt);
        }
        public bool ExecuteNonQuery(string _query)
        {
            DataSettingsManager _dataSettingsManager = new DataSettingsManager();
            DataSettings        _dataSettings        = _dataSettingsManager.LoadSettings();
            SqlConnection       _sqlConnection       = new SqlConnection(_dataSettings.DataConnectionString + ";Connection Timeout=0");
            DataTable           dt = new DataTable();

            if (_sqlConnection.State == ConnectionState.Closed)
            {
                _sqlConnection.Open();
            }
            SqlCommand _command = new SqlCommand(_query, _sqlConnection);

            try
            {
                _command.ExecuteNonQuery();
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
示例#4
0
 public EfDataProviderManager(DataSettings settings) : base(settings)
 {
 }
示例#5
0
 public MongoDBDataProviderManager(DataSettings settings) : base(settings)
 {
 }
        /// <summary>
        /// Load data settings
        /// </summary>
        /// <param name="filePath">File path; pass null to use the default settings file</param>
        /// <param name="reloadSettings">Whether to reload data, if they already loaded</param>
        /// <param name="fileProvider">File provider</param>
        /// <returns>Data settings</returns>
        public static DataSettings LoadSettings(string filePath = null, bool reloadSettings = false, INopFileProvider fileProvider = null)
        {
            if (!reloadSettings && Singleton <DataSettings> .Instance != null)
            {
                return(Singleton <DataSettings> .Instance);
            }

            fileProvider ??= CommonHelper.DefaultFileProvider;
            filePath ??= fileProvider.MapPath(NopDataSettingsDefaults.FilePath);

            //check whether file exists
            if (!fileProvider.FileExists(filePath))
            {
                //if not, try to parse the file that was used in previous nopCommerce versions
                filePath = fileProvider.MapPath(NopDataSettingsDefaults.ObsoleteFilePath);
                if (!fileProvider.FileExists(filePath))
                {
                    return(new DataSettings());
                }

                //get data settings from the old txt file
                var dataSettings = new DataSettings();
                using (var reader = new StringReader(fileProvider.ReadAllText(filePath, Encoding.UTF8)))
                {
                    string settingsLine;
                    while ((settingsLine = reader.ReadLine()) != null)
                    {
                        var separatorIndex = settingsLine.IndexOf(':');
                        if (separatorIndex == -1)
                        {
                            continue;
                        }

                        var key   = settingsLine.Substring(0, separatorIndex).Trim();
                        var value = settingsLine.Substring(separatorIndex + 1).Trim();

                        switch (key)
                        {
                        case "DataProvider":
                            dataSettings.DataProvider = Enum.TryParse(value, true, out DataProviderType providerType) ? providerType : DataProviderType.Unknown;
                            continue;

                        case "DataConnectionString":
                            dataSettings.ConnectionString = value;
                            continue;

                        default:
                            dataSettings.RawDataSettings.Add(key, value);
                            continue;
                        }
                    }
                }

                //save data settings to the new file
                SaveSettings(dataSettings, fileProvider);

                //and delete the old one
                fileProvider.DeleteFile(filePath);

                Singleton <DataSettings> .Instance = dataSettings;
                return(Singleton <DataSettings> .Instance);
            }

            var text = fileProvider.ReadAllText(filePath, Encoding.UTF8);

            if (string.IsNullOrEmpty(text))
            {
                return(new DataSettings());
            }

            //get data settings from the JSON file
            Singleton <DataSettings> .Instance = JsonConvert.DeserializeObject <DataSettings>(text);

            return(Singleton <DataSettings> .Instance);
        }