使用 C# 导出 SQLite 数据库,最佳实践
我正在尝试为我的数据库应用程序实现导出功能。我正在使用 C#,并且拥有我正在使用的 C# 的 SQlite 包装器。请原谅我对数据库概念/常见用法的无知,这是我的第一个数据库应用程序。
导出: SQLite 数据库很容易存储在单个 s3db 文件中。那么,要导出,我只需要使用 System.IO.File.Copy 复制该文件即可?这是最好的方法吗?最后,对于导出,我是否需要在复制文件之前关闭所有数据库连接?同样,针对这种情况的最佳实践将会有很大帮助......我不确定这种情况下的一些事情。例如...
如果数据库很大并且复制需要一段时间怎么办?用户可以开始导出,然后立即尝试将某些内容插入数据库,这显然是一个问题。复制时是否应该锁定程序?
一如既往,感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为 SQL Server CE 做了类似的功能,它在概念上与 sqlite 非常相似。最安全的处理方法是您建议的方式:关闭所有数据库连接并阻止用户再打开(我使用整个应用程序上显示备份进度的模式窗口来处理此问题)。再说一遍,我本身并没有使用 sqlite 完成此操作,但使用 SQL Server CE,它只是一个简单的文件复制。
要在 WinForms 应用程序的整个应用程序上创建模式窗口,只需使用 MessageBox.Show(),并且不指定所有者。
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.
复制是最简单的方法,但我不确定这是最好的方法。除了必须确保在复制过程中锁定文件之外,您可能还希望将数据转储为比您正在使用的特定 SQLite 版本的二进制表示形式更便携的格式。
将所有数据转储到文件中就像复制一样简单。
例如,请参阅本页中的“将整个数据库转换为 ASCII 文本文件”。
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.