public bool SaveStockQuote(stock_quotes quote, out string errorMessage)
        {
            bool success = true;

            errorMessage = String.Empty;
            try
            {
                entities.stock_quotes.Add(quote);
                entities.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.Message.ToString());
                errorMessage = e.InnerException.InnerException.Message;
                success      = false;
            }
            return(success);
        }
示例#2
0
        public static void EditQuoteFile(OpenFileDialog fileDialog, DateTime date)
        {
            StockQuoteRepository quoteRepo = new StockQuoteRepository();
            StockRepository      stockRepo = new StockRepository();
            stock_quotes         quote;
            bool      success = false;
            bool      flag    = true;
            String    ErrorMessage;
            AwaitForm waitForm = new AwaitForm();
            string    fileName = fileDialog.FileName;

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook  xlWorkbook  = xlApp.Workbooks.Open(fileName);
            Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Range     xlRange     = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            if (xlRange[2, 2].Value2 != null)
            {
                for (int i = 2; i <= rowCount; i++)
                {
                    string stockCode = xlRange[i, 1].Value2.ToString().ToUpper().Trim();
                    int    stock_id  = stockRepo.GetStockId(stockCode);
                    if (stockCode.Length == 4)
                    {
                        bool isExisted = stockRepo.CheckCurrentStock(stockCode);
                        if (!isExisted)
                        {
                            stock stock = new stock();
                            stock.code     = stockCode;
                            quote          = new stock_quotes();
                            quote.stock_id = stockRepo.SaveStock(stock, out ErrorMessage);
                        }

                        quote              = quoteRepo.FindStockQuoteByStockIdAndDate(stock_id, date);
                        quote.quote_date   = date.Date;
                        quote.open         = xlRange.Cells[i, 3].Value2 == 0 ? Decimal.Parse(xlRange.Cells[i, 2].Value2.ToString()) : Decimal.Parse(xlRange.Cells[i, 3].Value2.ToString());
                        quote.high         = Decimal.Parse(xlRange.Cells[i, 4].Value2.ToString());
                        quote.low          = Decimal.Parse(xlRange.Cells[i, 5].Value2.ToString());
                        quote.close        = Decimal.Parse(xlRange.Cells[i, 6].Value2.ToString());
                        quote.volume       = Decimal.Parse(xlRange.Cells[i, 7].Value2.ToString()) / 100;
                        quote.value        = Decimal.Parse(xlRange.Cells[i, 8].Value2.ToString());
                        quote.frequency    = Decimal.Parse(xlRange.Cells[i, 9].Value2.ToString());
                        quote.foreign_sell = Decimal.Parse(xlRange.Cells[i, 10].Value2.ToString()) / 100;
                        quote.foreign_buy  = Decimal.Parse(xlRange.Cells[i, 11].Value2.ToString()) / 100;

                        success = quoteRepo.UpdateStockQuote(quote, out ErrorMessage);
                        if (!String.IsNullOrEmpty(ErrorMessage))
                        {
                            MessageBox.Show(ErrorMessage);
                        }
                        if (flag)
                        {
                            waitForm.Show();
                            waitForm.Refresh();
                        }

                        flag = false;
                    }
                }
                if (success)
                {
                    waitForm.Close();
                    MessageBox.Show("Data berhasil disimpan");
                }
            }
            else
            {
                MessageBox.Show("File tidak memiliki data");
            }
        }
        public stock_quotes FindStockQuoteByStockIdAndDate(int stock_id, DateTime date)
        {
            stock_quotes quote = entities.stock_quotes.Where(q => q.stock_id == stock_id).Where(q => q.quote_date == date.Date).First();

            return(quote);
        }
        public stock_quotes FindStockQuoteById(int id)
        {
            stock_quotes quote = entities.stock_quotes.Where(q => q.id == id).First();

            return(quote);
        }