简体   繁体   中英

Exporting a SQLite database using C#, best practices

I'm trying to implement the export functionality for my database application. I'm using C# and have the SQlite wrapper for C# that I'm using. Pardon my ignorance of database concepts/common usage, this is my first database application.

Export : Easily enough, the SQLite databases are stored in a single s3db file. So, to export, I simply need to copy that file using System.IO.File.Copy? Is that the best way to do this? Lastly, with export, do I need to close all database connections before copying the file? Again, best practices for this situation would be a great help... I'm unsure as to a few things in this situation. For example...

What if the database is huge and the copy takes a while? The user could start a export, then immediately go and try to insert something into the database, which would obviously be an issue. Should I lock the program while copying?

As always, thank you for any assistance.

I did similar functionality for SQL Server CE, which is very similar to sqlite in concept. The safest way to handle it is how you suggested: Close all database connections and prevent the user from opening anymore (I handled this with a modal window over the entire application that showed progress of the backup). Again, I haven't done it with sqlite per se, but with SQL Server CE, it was a simple file copy.

To make a window modal over the entire application for a WinForms app, simply use MessageBox.Show(), and do not specify an owner.

The advantage of copying the file is simply that the alternative, recreating the database and inserting the records, will assuredly take a lot more time. You should definitely lock that operation if your app allows changes to either the source or the export.

Copying is the simplest way, but I'm not sure it's the best way. Aside from having to make sure you're locking the the file during copying, you might want to dump the data into a format that's a bit more portable than just the binary representation of the specific version of SQLite you're using.

Dumping all data into a file is just as easy as copying.

See, for example: "Converting An Entire Database To An ASCII Text File" in this page .

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