B M*_*ter 5 c# sqlite sqlconnection
我当前正在映射网络驱动器并以这种方式连接到文件 (Z:\Data\Database.db)。我希望能够在连接字符串中使用相对路径 (\server\Data\Database.db),但它给了我一个 SQLite 错误“无法打开数据库文件”。检查Directory.Exists(\\server\Data\Database.db);返回 true。
以下是使用路径“\\server”作为参数打开连接的尝试:
public static OpenDB(string dbPath)
{
using (SQLiteConnection conn = new SQLiteConnection($"Data Source={Path.Combine(dbPath, "Data\\Database.db")}"))
{
if (dbPath != null && dbPath != "")
{
try
{
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unable to Open Database", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的解决方案。这是jdweng和Shawn的建议的结合。首先,我将该路径设置dbPath为管理共享驱动器。从那里我让程序从管理共享创建数据库的临时本地副本:
private static void MakeTempDatabaseCopy(string dbPath, string exePath)
{
try
{
File.Copy(Path.Combine(dbPath, "Data", "Database.db"), Path.Combine(exePath, "Temp", "Database.db"), true);
FileInfo directoryInfo = new FileInfo(Path.Combine(exePath, "Temp", "Database.db"));
directoryInfo.Attributes = FileAttributes.Temporary;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Retrieving Database", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Run Code Online (Sandbox Code Playgroud)
之后,所有方法都可以从本地副本中读取。由于File.Copy()使用布尔值,因此任何需要刷新数据库的操作都可以使用管理共享中的新副本覆盖本地副本。希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
1328 次 |
| 最近记录: |