.NET平台使用spatilite扩展
源码github地址在此,记得点星:
https://github.com/brandonxiang/SpatialiteSharp
当然你先在NuGet上引用SQLite的安装包。然后就是扩展,你当然可以尝试采用类似python的方法——将mod_spatialite.dll的包设为环境变量,然后enable_extension
之类。但是很麻烦,不适合数据迁移。
参考SpatialiteSharp,在NuGet上引用。一行代码
SpatialiteLoader.Load(conn);
解决战斗。看看源码,其实很简单,就是判断X86还是X64,后面就是一样的。
public class SpatialiteLoader
{
private static readonly object Lock = new object();
private static bool _haveSetPath;
/// <summary>
/// Loads mod_spatialite.dll on the given connection
/// </summary>
public static void Load(SQLiteConnection conn)
{
lock (Lock)
{
//Need to work out where the file is and add it to the path so it can load all the other dlls too
if (!_haveSetPath)
{
var spatialitePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), (Environment.Is64BitProcess ? "x64" : "x86"), "spatialite");
Environment.SetEnvironmentVariable("PATH", spatialitePath + ";" + Environment.GetEnvironmentVariable("PATH"));
_haveSetPath = true;
}
}
conn.LoadExtension("mod_spatialite.dll");
}
}
但是这个包在ASP NET中不能很好的运行,因为.NET的路径问题,不过将路径改成AppDomain.CurrentDomain.BaseDirectory
即可。具体参考我的Github
转载,请表明出处。总目录Awesome GIS