【发布时间】:2021-10-20 20:40:07
【问题描述】:
我正在尝试将我的应用程序从访问 SQLite 切换过来,并且我喜欢加密。我正在尝试将“Microsoft.data.sqlite”与“SQLitePCLRaw.bundle_e_sqlcipher”一起使用,但设置密码似乎无济于事。按照官方指南:from here
它是这样设置的:Public DBConnection As String = "Data Source=" & Environment.CurrentDirectory & "\test.db;Mode=ReadWriteCreate;Password=testtest123" 并打开了连接,我也尝试这样做:
Dim DBConnection As String = "Data Source=" & Environment.CurrentDirectory & "\test.db;Mode=ReadWriteCreate"
Try
Using dbconn As New SqliteConnection(DBConnection)
dbconn.Open()
Dim command = dbconn.CreateCommand()
command.CommandText = "SELECT quote($password);"
command.Parameters.AddWithValue("$password", "testtest123")
Dim quotedpsw = New String(command.ExecuteScalar)
command.CommandText = "PRAGMA key = " & quotedpsw
command.Parameters.Clear()
command.ExecuteNonQuery()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
我在使用连接之前尝试设置SQLitePCL.raw.SetProvider(New SQLitePCL.SQLite3Provider_e_sqlcipher())这些方法,它们都没有加密它,也没有给出异常。
我也尝试使用“SQLitePCLRaw.bundle_sqlcipher”,但它给出了这个异常:找不到方法“Int32 SQLitePCL.ISQLite3Provider.sqlite3_win32_set_directory(Int32, System.String)”。'
我还尝试使用 SQLCipher 3 和 4 使用 DB Browser for SQLite 对外部数据库进行加密,但这些方式都无法从我的应用程序连接到数据库...谢谢!
编辑:我想我会重新启动,所以我删除了每个包并重新下载了我最初提到的两个(+提供程序包),现在当我设置 SQLitePCL.raw.SetProvider(New SQLitePCL.SQLite3Provider_e_sqlcipher()) 时,每次它给出一个异常:“e_sqlcipher”DLL无法加载。找不到指定的模块。如果有人对另一个解决方案有很好的经验,我也愿意使用其他解决方案。
【问题讨论】:
-
OT,我建议不要使用
Environment.CurrentDirectory。您可能假设这将返回包含 EXE 的文件夹的路径,但不一定是这种情况,它实际上可以在会话期间更改。如果您想要程序文件夹路径,则在 WinForms 中使用Application.StartupPath。也就是说,虽然我不能 100% 确定它是否受到该 SQLite 提供程序的支持,但我认为您可以将路径硬编码为"Data Source=|DataDirectory|\test.db"。这肯定适用于 Access 或 SQL Server Express。 -
谢谢,我已经对其进行了测试,它返回数据的方式与我在操作中的方式没有问题。我这样做只是为了测试,但我将对其进行硬编码以排除这种情况。
标签: vb.net sqlite microsoft.data.sqlite