1
我有一個Windows服務,當開始去我定義和收集文件列表的路徑,每個文件都有記錄在我插入數據庫(甲骨文)。我的問題是,如果這個數據庫下來或如果有一些例外我將如何回滾從文件插入之前的紀錄.. 對於實例如何在Windows Service中回滾?
我有10個文件,每個文件都有5條 7解析文件時存在像第四記錄某種異常( db down或者其他)我想回滾從第7個文件插入的前3條記錄,有可能嗎?
我的代碼是在這裏..
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");
}
其中i上傳數據的方法:
Upload obj = new Upload();
var files = db.SWIFT_FILES.Select(x => 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++;
}
在迅速的解析,我只是解析文件,並從中收集數據並保存在數據庫中它實際上是一個很長的方法,所以我不想在這裏混亂
總之 我做了一個表的對象,並保存在它
for(int i=0; i<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();
}
請告訴我什麼是這種或任何建議的解決方法將幫助完整。 我正在使用Oracle數據庫和c#窗口服務和實體框架5
我得到InvalidoperationException說連接不是打開..任何想法?你的解決方案看起來不錯@JanneP –
對不起,我編輯了我的答案,包括連接開放。 – JanneP
另一個例外是Dbcontext已經處置 –