示例#1
0
        private void newCSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                List <TransactionItem> newItems = new List <TransactionItem>();
                string   account  = string.Empty;
                DateTime lastDate = DateTime.MinValue;

                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                    string headerLine = sr.ReadLine();
                    string line       = string.Empty;
                    while (!string.IsNullOrEmpty(line = sr.ReadLine()))
                    {
                        while (line.Where(c => c == '"').Count() % 2 == 1) // Read until all quotes are matched
                        {
                            line += sr.ReadLine();
                        }
                        TransactionItem item = null;
                        try
                        {
                            item = TransactionItem.ImportItem(line, headerLine);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("File import aborted: \r\n{0}\r\n{1}", ex.Message, line));
                            return;
                        }
                        if (item.TransDate > lastDate)
                        {
                            lastDate = item.TransDate;
                        }
                        if (string.IsNullOrEmpty(account))
                        {
                            account = item.Account;
                        }
                        if (TransactionItem.GetCollection().Exists(t => t.Equals(item)))
                        {
                            continue;
                        }
                        newItems.Add(item);
                    }
                    TransactionItem.GetCollection().InsertBulk(newItems);
                }

                BackupTransactionFile(openFileDialog1.FileName, account, lastDate);

                InitGrids();
            }
        }