Ejemplo n.º 1
0
        private QuoteData ReadNextQuoteFromStream()
        {
            if (reader == null)
            {
                return(null);
            }
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (line.Length == 8)
                {
                    var year  = line.Substring(4).ToIntSafe();
                    var month = line.Substring(2, 2).ToIntSafe();
                    var day   = line.Substring(0, 2).ToIntSafe();
                    if (!year.HasValue || !month.HasValue || !day.HasValue)
                    {
                        continue;
                    }
                    isNewFormat = true;
                    curDate     = new DateTime(year.Value, month.Value, day.Value);
                    continue;
                }

                // распарсить строку
                var quote = isNewFormat
                                ? QuoteData.ParseQuoteStringNewFormat(line, curDate)
                                : QuoteData.ParseQuoteStringOldFormat(line);
                return(quote);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// загрузить котировки из файла и сохранить их в БД, пачками
        /// </summary>
        private void BtnSaveInDbClick(object sender, EventArgs e)
        {
            var           commandsBlock      = new List <string>();
            const int     maxCommandsInBlock = 50;
            var           ticker             = tbTickerId.Text.ToInt();
            SqlConnection connection         = null;
            var           connectionString   = ConfigurationManager.ConnectionStrings["QuoteBase"].ConnectionString;
            var           startTime          = dpStart.Value;
            var           endTime            = dpEnd.Value;

            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            try
            {
                var newFormat = false;
                connection = new SqlConnection(connectionString);
                connection.Open();
                using (var sr = new StreamReader(openFileDialog.FileName))
                {
                    DateTime?date = null;
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        DateTime?fileDate;
                        var      isNew = QuoteData.IsNewFormatDateRecord(line, out fileDate);
                        if (fileDate.HasValue)
                        {
                            date = fileDate;
                        }
                        if (isNew)
                        {
                            newFormat = true;
                            continue;
                        }
                        var quote = newFormat ? QuoteData.ParseQuoteStringNewFormat(line, date.Value) :
                                    QuoteData.ParseQuoteStringOldFormat(line);
                        if (quote == null)
                        {
                            continue;
                        }
                        if (quote.time < startTime)
                        {
                            continue;
                        }
                        if (quote.time > endTime)
                        {
                            break;
                        }

                        var cmd = MakeInsertQuoteCmd(ticker, quote);
                        commandsBlock.Add(cmd);
                        if (commandsBlock.Count > maxCommandsInBlock)
                        {
                            ExecuteSaveCommands(commandsBlock, connection);
                            commandsBlock.Clear();
                        }
                    }
                }
                if (commandsBlock.Count > 0)
                {
                    ExecuteSaveCommands(commandsBlock, connection);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Ошибка сохранения в БД", ex);
                MessageBox.Show(string.Format("Ошибка сохранения в БД: {0}", ex.Message));
                return;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
            MessageBox.Show("Котировки сохранены в БД");
        }