2016-10-07 28 views
0

ジオメトリ(POINT、LINE、POLYGON)をsqlite DB(spatialite)のテーブルに挿入しようとしていますが、ジオメトリの列は常にNULLです。spatialiteのジオメトリを挿入すると常にNULLが挿入されますC#

INSERT後のDB内の情報。
どのようにすべき

public SQLiteConnection create_Open_SpatilaDB(string dbName) 
{ 
    try 
    { 
     dbName += DateTime.Now.ToString("ddMMyyyyHHmmssff") + ".sqlite"; 

     string ruta = HttpContext.Current.Server.MapPath("/Uploads/" + dbName); 

     string strConexion = "Data Source=" + ruta + ";Version=3;"; 

     SQLiteConnection conexion = null; 

     if (!File.Exists(ruta)) 
     { 
      // create the connection 
      conexion = new SQLiteConnection(strConexion); 

      //Abrir la conexión 
      conexion.Open(); 

      //Load the lib 
      conexion.EnableExtensions(true); 
      conexion.LoadExtension("libspatialite-2.dll");      

      //Create tables 
      string SQL = "CREATE TABLE AppVersion (" 
         + " versionCode INTEGER NOT NULL PRIMARY KEY," 
         + " state INTEGER NOT NULL);"; 

      SQLiteCommand cmd = new SQLiteCommand(SQL, conexion); 
      cmd.ExecuteNonQuery(); 

      SQL = "CREATE TABLE Table1 ("       
       + " Codigo TEXT NOT NULL PRIMARY KEY," 
       + " Nombre TEXT," 
       + " Geometry POLYGON);"; 

      cmd = new SQLiteCommand(SQL, conexion); 
      cmd.ExecuteNonQuery(); 

      SQL = "CREATE TABLE Table2(" 
        + " Codigo TEXT NOT NULL PRIMARY KEY," 
        + " Nombre TEXT NOT NULL," 
        + " Geometry POIN);"; 

      cmd = new SQLiteCommand(SQL, conexion); 
      cmd.ExecuteNonQuery(); 
      . 
      . 
      . 
     } 
     else 
     { 
      conexion = new SQLiteConnection(strConexion); 
      conexion.Open(); 
      conexion.EnableExtensions(true); 
      conexion.LoadExtension("libspatialite-2.dll"); 
     } 

     return conexion; 

    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

文字列のSQLの結果はOKですが、私はこれは、C#の私のコードです

INSERT INTO Table1(Codigo, Nombre, Geometry) VALUES('1','Andalucia',GeomFromText('POLYGON((-75.8396284742612 8.39962954808519, ..., -75.8396284742612 8.39962954808519))', 4326)); 

INSERT INTO Table2(Codigo, Nombre, Geometry) VALUES('1','Andalucia',GeomFromText('POINT(-75.8361480385065 8.40883548152381)', 4326)); 

を使用しています

ここに私は情報を挿入しています:

public void insertInfo(SQLiteConnection conexion) 
{ 
    try 
    { 
     string SQL = "BEGIN"; 
     SQLiteCommand cmd = new SQLiteCommand(SQL, conexion); 
     cmd.ExecuteNonQuery(); 

     //Insert info table1   
     List<Table1> list = getInfoTable1(); 

     foreach (Table1 item in list) 
     { 
      SQL = "INSERT INTO Table1(" 
        + " Codigo," 
        + " Nombre," 
        + " Geometry) VALUES(" 
        + "'" + item.Codigo + "'," 
        + "'" + item.Nombre + "'," 
        + "GeomFromText(" + item.geom + "));"; 

      cmd = new SQLiteCommand(SQL, conexion); 
      cmd.ExecuteNonQuery(); 
     } 

     //Insert info table1   
     List<Table2> list = getInfoTable2(); 

     foreach (Table2 item in list) 
     { 
      SQL = "INSERT INTO Table2(" 
        + " Codigo," 
        + " Nombre," 
        + " Geometry) VALUES(" 
        + "'" + item.Codigo + "'," 
        + "'" + item.Nombre + "'," 
        + "GeomFromText(" + item.geom + "));"; 

      cmd = new SQLiteCommand(SQL, conexion); 
      cmd.ExecuteNonQuery(); 
     } 
     . 
     . 
     . 
     SQL = "COMMIT"; 
     cmd = new SQLiteCommand(SQL, conexion); 
     cmd.ExecuteNonQuery(); 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

答えて

0

私はあなたのテーブルの列をジオメトリ列としてマークする必要があると思います。それ以外の場合は、ジオメトリ列として認識されません。

これは、通常の列をジオメトリ列に変換するためにRecoverGeometryColumn-Functionを使用する必要があります。あなたは本当にあなたがSystem.Data.SQLite ADOでSpatialite> 4.2.0を検討すべきであるのC#を使用したい場合は
https://www.gaia-gis.it/gaia-sins/spatialite-cookbook/html/new-geom.html

さらに: は、そうでなければ、あなたもこのリンクで見てくださいAddGeometryColumn-機能 を使用することができます。 SQLite用のNETプロバイダ。
ここでこの説明をご覧ください:
https://groups.google.com/forum/#!topic/spatialite-users/u2QZpQL_6ek

関連する問題