public async Task <IHttpActionResult> DB(string fileName)
        {
            Log.Info("Request for {0}", Request.RequestUri);
            db = new Entities();
            using (var transaction = db.Database.BeginTransaction())
                using (db)
                {
                    db.Configuration.AutoDetectChangesEnabled = false;
                    try
                    {
                        await Task.Run(() =>
                        {
                            StructHelper structHelper = new StructHelper(fileName);
                            structHelper.Open(FileAccess.Read, FileShare.None);

                            var header = (Header)structHelper.GetNextStructureValue(typeof(Header));

                            int count = 0;
                            object record;
                            while ((record = structHelper.GetNextStructureValue(typeof(StructModels.TradeRecord))) != null)
                            {
                                var rec    = (StructModels.TradeRecord)record;
                                var recObj = new TradeRecord
                                {
                                    Id       = rec.id,
                                    FileName = fileName,
                                    Account  = rec.account,
                                    Volume   = rec.volume,
                                    Comment  = rec.comment,
                                    Version  = header.version,
                                    Type     = header.type
                                };

                                ++count;
                                db = AddToContext(db, recObj, count, numberOfBulkSave, true);
                            }

                            //Thread.Sleep(3000);
                        }).ConfigureAwait(false);

                        db.SaveChanges();
                        transaction.Commit();
                    }
                    catch (FileNotFoundException ex)
                    {
                        transaction.Rollback();
                        Log.Warn("Exception message {0}. Stack Trace: {1}", ex.Message, ex.StackTrace);
                        return(BadRequest(ex.Message));
                    }
                    catch (DbUpdateException ex)
                    {
                        transaction.Rollback();
                        Log.Error("Exception message {0}. Stack Trace: {1}", ex.Message, ex.StackTrace);
                        return(InternalServerError(ex));
                    }
                }
            return(Ok());
        }
Пример #2
0
        public static void ExecuteProcessCSV(string fileName, string csvFileName)
        {
            StructHelper structHelper = new StructHelper(fileName);

            structHelper.Open(FileAccess.Read, FileShare.None);

            using (StreamWriter output = new StreamWriter(csvFileName))
            {
                var header = structHelper.GetNextStructureValue(typeof(Header));
                if (header is Header)
                {
                    var head = (Header)header;
                    output.WriteLine(string.Format("{0},{1}", head.version, head.type));
                }

                object record;
                while ((record = structHelper.GetNextStructureValue(typeof(StructModels.TradeRecord))) != null)
                {
                    var rec = (StructModels.TradeRecord)record;
                    output.WriteLine(string.Format("{0},{1},{2},{3}", rec.id, rec.account, rec.volume, rec.comment));
                }
            }
        }
Пример #3
0
        public static void CreateBinaryFile(string fileName)
        {
            StructHelper structHelper = new StructHelper(fileName);

            structHelper.Create(FileAccess.ReadWrite, FileShare.None);
            var collectionOfStruct = new List <object>();
            var header             = new Header {
                version = 1, type = "type1"
            };

            collectionOfStruct.Add(header);
            var tradeRecord = new StructModels.TradeRecord {
                id = 1, volume = 12.1, account = 2, comment = "comment"
            };

            collectionOfStruct.Add(tradeRecord);
            var tradeRecord1 = new StructModels.TradeRecord {
                id = 2, volume = 12.1, account = 2, comment = "comment"
            };

            collectionOfStruct.Add(tradeRecord1);
            structHelper.WriteStructureCollection(collectionOfStruct);
            structHelper.Close();
        }