简体   繁体   English

使用C#导出SQLite数据库的最佳实践

[英]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. 我正在使用C#,并具有我正在使用的C#的SQlite包装器。 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. 导出: SQLite数据库很容易存储在单个s3db文件中。 So, to export, I simply need to copy that file using System.IO.File.Copy? 因此,要导出,我只需要使用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. 我为SQL Server CE做过类似的功能,在概念上与sqlite非常相似。 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. 同样,我本身还没有使用sqlite完成此操作,但是使用SQL Server CE,它是一个简单的文件副本。

To make a window modal over the entire application for a WinForms app, simply use MessageBox.Show(), and do not specify an owner. 要使整个WinForms应用程序的窗口成为模态窗口,只需使用MessageBox.Show(),而不指定所有者。

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. 除了必须确保在复制过程中锁定文件之外,您可能还希望将数据转储为一种格式,该格式比您所使用的特定SQLite版本的二进制表示形式更具可移植性。

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 . 参见,例如: 本页中的 “将整个数据库转换为ASCII文本文件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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