我当前正在映射网络驱动器,并以这种方式连接到文件(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);
}
}
}
}
答案 0 :(得分:0)
This was the solution that I used. It is a combination of advice from jdweng and Shawn. First I made the path dbPath
be an administrative share drive. From there I made the program make a temporary local copy of the database from the administrative share:
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);
}
}
After that all methods can read from the local copy. Since the File.Copy()
uses the Boolean, anything that requires a refresh of the database can just overwrite the local copy with a fresh copy from the administrative share. Hope this helps!