title: How to roll back in windows Service?
body: <p>I have a windows service which when start goes to the path which i define and collect a list of files and each file have records in it which i insert in database(Oracle).My problem is if this database gets down or if there is some exception how will i roll back previous record which inserted from the file..
  For Example</p>
  
  <p>i have 10 files each file has 5 records
  when parsing 7th file there is some kind of exception on the 4th record like(db down or anything) i want to roll back previous 3 records which was inserted from 7th file, is it possible?</p>
  
  <p>My code is here..</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      timer1 = new Timer();
      this.timer1.Interval = 30000;
      this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_tick);
      timer1.Enabled = true;
  
      Upload.WriteErrorMessage("Service Started");
  
  }
  
  private void timer_tick(object sender,ElapsedEventArgs e)
  {
      timer1.Enabled = false;
      SwiftParser.Parse();
      timer1.Enabled = true;
  }
  
  protected override void OnStop()
  {
      timer1.Enabled = false;
      Upload.WriteErrorMessage("service stoped");
  }
  </code></pre>
  
  <p>the method where i uploading data:</p>
  
    <pre class="lang-cs prettyprint-override"><code>Upload obj = new Upload();
  var files = db.SWIFT_FILES.Select(x =&gt; x.LASTFILENUMBER).Max();
  int sFile = Convert.ToInt32(files);
  int sf1 = sFile;
  while (true)
  {
      string flName = @"D:\Projects\Recon\Nostro\swift\" + sFile.ToString("00000000").Trim() + ".swf";
      if (System.IO.File.Exists(flName))
      {
          obj.SwiftParse(flName);
      }
      else
      {
          break;
      }
      sFile++;
  }
  </code></pre>
  
  <p>In swift parse i just parse file and collect data from it and save in database it was actually a long method so i dont want to make mess here</p>
  
  <p>in short
  i make a object of table and save in it</p>
  
  <pre><code>for(int i=0; i&lt;length of record; i ++)
  {
      SwiftTable sw = new SwiftTable();
      sw.acc_no = from file i get acc no;
      sw.amount= ......................;
      sw.bank=.........................;
      db.Add.SwiftTable(sw);
      db.SaveChanges();
  }
  </code></pre>
  
  <p>Please tell me what are the workarounds for this or any suggestion will be help full.
  I am using Oracle databse and c# windows service and Entity framework 5 </p>