public bool CreateBatchStatusRecord(BatchStatus status)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(_connectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(GetInsertCommand()))
                    {
                        cmd.Connection = conn;
                        cmd.Parameters.Clear();
                        SetParameters(cmd, status);

                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException exc)
                        {
                            Console.WriteLine("Error writing " + status.ToString() + " to database.   Exception: " + exc.Message);
                            throw exc;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                return false;
            }

            return true;
        }
        public void RunDataLoader()
        {
            string currentClassName = this.GetType().Name;
            string batchrefrence = Initialize();
            string guid = Guid.NewGuid().ToString();

            BatchStatus status = new BatchStatus() { BatchName = currentClassName, BatchId = guid, BatchRefrence = batchrefrence, StartTime = DateTime.Now, Comment = "", Status = "Started" };
            _statusHandler.CreateBatchStatusRecord(status);

            try
            {

                SetStatusLoad(status);
                LoadData();

                SetStatusTransform(status);
                TransformData();

                SetStatusSaving(status);
                SaveData();

                SetStatusCleanUp(status);
                CleanUp();

                SetStatusFinished(status);

            }
            catch(DataLoaderException dx)
            {
                status.FinishTime = DateTime.Now;
                status.Status = dx.Status;
                status.Comment = dx.Message;
                _statusHandler.UpdateBatchStatusRecord(status);
            }

            catch (Exception ex)
            {
                status.FinishTime = DateTime.Now;
                status.Status = "Failed";
                status.Comment = ex.Message;
                _statusHandler.UpdateBatchStatusRecord(status);
            }
        }
 private void SetStatusFinished(BatchStatus status)
 {
     status.FinishTime = DateTime.Now;
     status.Status = "Finished";
     _statusHandler.UpdateBatchStatusRecord(status);
 }
 private void SetStatusCleanUp(BatchStatus status)
 {
     status.StartCleanupTime = DateTime.Now;
     status.Status = "Cleaning up";
     _statusHandler.UpdateBatchStatusRecord(status);
 }
 private void SetStatusTransform(BatchStatus status)
 {
     status.StartTransformTime = DateTime.Now;
     status.Status = "Transforming";
     _statusHandler.UpdateBatchStatusRecord(status);
 }
 private void SetStatusSaving(BatchStatus status)
 {
     status.StartSaveTime = DateTime.Now;
     status.Status = "Saving";
     _statusHandler.UpdateBatchStatusRecord(status);
 }
 private void SetStatusLoad(BatchStatus status)
 {
     status.StartLoadTime = DateTime.Now;
     status.Status = "Loading";
     _statusHandler.UpdateBatchStatusRecord(status);
 }
        private void SetParameters(SqlCommand cmd, BatchStatus status)
        {
            cmd.Parameters.Add("@batchname", System.Data.SqlDbType.NChar).Value = status.BatchName;
            cmd.Parameters.Add("@batchid", System.Data.SqlDbType.NChar).Value = status.BatchId;
            cmd.Parameters.Add("@start_time", System.Data.SqlDbType.DateTime).Value = status.StartTime;

            cmd.Parameters.Add("@batchrefrence", System.Data.SqlDbType.NChar).Value = status.BatchRefrence;

            if (status.StartLoadTime != null)
                cmd.Parameters.Add("@start_load_time", System.Data.SqlDbType.DateTime).Value = status.StartLoadTime;
            else
                cmd.Parameters.Add("@start_load_time", System.Data.SqlDbType.DateTime).Value = DBNull.Value;

            if (status.StartTransformTime != null)
                cmd.Parameters.Add("@start_transform_time", System.Data.SqlDbType.DateTime).Value = status.StartTransformTime;
            else
                cmd.Parameters.Add("@start_transform_time", System.Data.SqlDbType.DateTime).Value = DBNull.Value;

            if (status.StartSaveTime != null)
                cmd.Parameters.Add("@start_save_time", System.Data.SqlDbType.DateTime).Value = status.StartSaveTime;
            else
                cmd.Parameters.Add("@start_save_time", System.Data.SqlDbType.DateTime).Value = DBNull.Value;

            if (status.StartCleanupTime != null)
                cmd.Parameters.Add("@start_cleanup_time", System.Data.SqlDbType.DateTime).Value = status.StartCleanupTime;
            else
                cmd.Parameters.Add("@start_cleanup_time", System.Data.SqlDbType.DateTime).Value = DBNull.Value;

            if (status.FinishTime != null)
                cmd.Parameters.Add("@finish_time", System.Data.SqlDbType.DateTime).Value = status.FinishTime;
            else
                cmd.Parameters.Add("@finish_time", System.Data.SqlDbType.DateTime).Value = DBNull.Value;

            cmd.Parameters.Add("@comment", System.Data.SqlDbType.Text).Value = status.Comment;

            cmd.Parameters.Add("@status", System.Data.SqlDbType.NChar).Value = status.Status;
        }