简体   繁体   中英

Is it possible to prevent database files from being deleted/modified externally without having it opened in c#?

The situation:

I'm currently debugging code from my predecessor which handles sqlite databases (.db3 file format). In the software you can load one or several databases. General Information (getting all table names, getting all column names, etc) on these databases is handled directly from the files, but for actual data information the databases are copied to a in-memory database where data is queried from. So after the initial information is gathered and all data is saved to the memory database, the actual files are not used anymore.

However, they are still represented on the GUI and referenced, if the user adds/deletes databases from the list.

The problem:

All of this leads to the following problem: If a user modifies/deletes the file of a loaded database (which is possible, because we don't have an open connection to the files), the internal database pool of the software differs from the actual file list. As an effect, we have undefined behavior in such a case, as soon as the list of used databases is updated.

possible solutions(?)

  • Probably the best thing to do, would be to rework the complete code and keep everything within the memory-database (GUI representation, data, database information,...). In this case I wouldn't need to worry about the files at all. However this would mean a major rework and...no time for that^^
  • Just open a sqlite-connections to all files and leave them open, as long as the respective in-memory database is listed as 'used'(not really a good option in my opinion).
  • Find a way to prevent files from being deleted/modified without having them opened. I'm not sure if/how that would be possible, what I have read so far is the one would need to change accessibility rights for the files programmatically which leads to several issues concerning user-rights in windows...

The question: Is there another way/suggestion for this issue? For me the best/easiest approach would be to find a way to just prevent a list of files from being modified. So far I haven't found a good solution for that approach.

Keeping a connection open to a SQLite database file will prevent Windows from deleting it.

A poor but quick solution could be to allow the original code to read the file to memory, then to grab a new connection to the file. You can keep this connection open without doing anything with it, preventing it from being deleted. Once the user of the application is done using that dataset, you dispose of the connection, allowing the file to be deleted again.

I would not recommend this - it's a dirty fix. It may however give you more time to properly refactor the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM