以下代码适用于 System.Data.Sqlite。最近我切换到 Microsoft.Data.Sqlite,它给出错误“Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: '无法打开数据库”
using (var command = _conn.CreateCommand())
{
var qAttach = @"ATTACH DATABASE 'file:C:\mydata\address.db?immutable=1' AS address";
command.CommandText = qAttach;
command.ExecuteNonQuery();
}
事实证明,您必须在Data Source
关键字中使用
URI文件名,以便设置SQLITE_OPEN_URI标志并启用与
ATTACH
语句一起使用。
Microsoft.Data.Sqlite 文档 可能应该指出这一点,但没有;它只说:This keyword also supports URI Filenames
。
var connectionString = "Data Source=file:foo.db?immutable=true";
using (var conn = new Microsoft.Data.Sqlite.SqliteConnection(connectionString))
{
conn.Open();
var attachCommandSql = "ATTACH DATABASE 'file:bar.db?immutable=true' as bar;";
var attachCommand = new SqliteCommand(attachCommandSql, conn);
attachCommand.ExecuteNonQuery();
}