Last active
April 18, 2017 04:31
-
-
Save glennblock/3c9e36d8da42c16d1add6723b6fe05a4 to your computer and use it in GitHub Desktop.
scriptcs sqlite extension test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data.SQLite; | |
public class SpatialScript | |
{ | |
DatabaseAdministrationClass _DatabaseAdministration = null; | |
public DatabaseAdministrationClass DatabaseAdministration | |
{ | |
get | |
{ | |
return _DatabaseAdministration == null ? new DatabaseAdministrationClass() : _DatabaseAdministration; | |
} | |
} | |
public class DatabaseAdministrationClass | |
{ | |
public bool CreateSpatialiteDatabase(string output_path, string filename) | |
{ | |
try | |
{ | |
if (!Directory.Exists(output_path)) | |
throw new Exception("Output folder '" + output_path + "' does not exists!"); | |
if (!filename.ToLower().Contains(".sqlite")) | |
filename = filename + ".sqlite"; | |
var fullpath = output_path + "\\" + filename; | |
if (File.Exists(output_path + "\\" + filename)) | |
throw new Exception("File already exists!"); | |
SQLiteConnection.CreateFile(fullpath); | |
var connStr = @"Data Source=" + fullpath + "; Version=3;"; | |
var currentDirectory = Environment.CurrentDirectory; | |
Environment.CurrentDirectory = Environment.CurrentDirectory + "/scriptcs_bin"; | |
using (var cnn = new SQLiteConnection(connStr)) | |
{ | |
cnn.Open(); | |
cnn.EnableExtensions(true); | |
cnn.LoadExtension(@"mod_spatialite.dll"); | |
//using (SQLiteCommand mycommand = new SQLiteCommand("SELECT load_extension(\"scriptcs_bin\\mod_spatialite.dll\")", cnn)) | |
//{ | |
// mycommand.ExecuteNonQuery(); | |
//} | |
// converts sqlite database to spatialite database | |
using (SQLiteCommand mycommand = new SQLiteCommand("SELECT InitSpatialMetaData(1)", cnn)) | |
{ | |
mycommand.ExecuteNonQuery(); | |
} | |
} | |
Environment.CurrentDirectory = currentDirectory; | |
return true; | |
} | |
catch(Exception ex) | |
{ | |
throw new Exception(ex.Message); | |
} | |
} | |
public bool CreatePostGisDatabase() | |
{ | |
return false; | |
} | |
public bool CreateFileGeodatabase(string output_path, string filename) | |
{ | |
return false; | |
} | |
} | |
} | |
var flder = @"c:\src\donnyv\"; | |
var dbfile = "test.sqlite"; | |
var ss = new SpatialScript(); | |
Console.WriteLine(ss); | |
File.Delete(flder + "\\" + dbfile); | |
Console.WriteLine(ss.DatabaseAdministration.CreateSpatialiteDatabase(flder,dbfile)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment