2

I created a C# application, this application creates a database on first launch, this database which is an SQLiteDatabase will be stored in the project folder. The process of filling this database takes on a mid-range notebook around 5 to 10 seconds. So it will be slower on low-budget notebooks. There isn't a big chance, but what if somebody force closes the application, while the database is being created? Or what if a power failure occurs? The result of this will come out in a broken database, which will never work.

I'am creating my Database in c# and also fill it from c#, this happens within a try and catch, should I delete the Database whenever the code within the catch is called? My applications checks at the beginning of the application whether there already is a database yes or no? if there isn't it will be created.

What should I do? Delete the database when an error occurs(code goes trough catch) or does somebody has a better solution for my problem?

Thanks in advance!

EDIT:

Some answers has been given to me:

I saw the following code, how can I use this code? Should I put the code that creates my database within the CallAMethodThatDoesSomeWork method?

using(TransactionScope tran = new TransactionScope()) {
    CallAMethodThatDoesSomeWork();
    CallAMethodThatDoesSomeMoreWork();
    tran.Complete();
}
Max
  • 12,622
  • 16
  • 73
  • 101
  • Why don't you use begin and commit transaction? if any error occurs, rollback transaction. – Monie corleone Mar 06 '13 at 08:01
  • 1
    Yes, put all your code inside the TransactionScope, and leave tran.Complete() at the end. – Dejo Mar 06 '13 at 08:22
  • Is, it really that easy? Thanks for your help:) One more little question, I'am still running my code between the try braces, can I delete them now? – Max Mar 06 '13 at 08:24

3 Answers3

2

You should use transactions and creating all commands that uses database under it. If application crashes, nothing will be created. This is a standard approach. See, for example here

Community
  • 1
  • 1
Dejo
  • 2,078
  • 3
  • 26
  • 38
2

Sqlite seems to support transactions. So, open a transaction, then populate the data, and only COMMIT when it's all done.

Whenever your application starts, you can surely check whether there's no data in the database (in which case, something went wrong the last time you tried to populate it).

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

Why don't you use begin and commit transaction? if any error occurs, rollback transaction.

For more on transactions refer link below.

http://www.sqlite.org/lang_transaction.html

Monie corleone
  • 1,618
  • 1
  • 16
  • 37