Connecting to Spatialite by a .NET application

2,139 views
Skip to first unread message

Axel

unread,
Mar 26, 2009, 1:41:52 PM3/26/09
to SpatiaLite Users
Hi all,
i'm trying to develope a simple demonstrative application in Visual C#
that connects to a SpatiaLite database and make some spatial query.
I'm using the System.Data.SQLite wrapper and i actually able to
connect to the SQLite database, but when i try to load the
libspatialite-2.dll library i receive this error:


Eccezione non gestita: System.Data.SQLite.SQLiteException: SQLite
error
not authorized
in System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
in System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
in System.Data.SQLite.SQLiteDataReader.NextResult()
in System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,
CommandBehavi
or behave)
in System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior
behavior)
in System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
in test.Program.Main(String[] args) in D:\Visual Studio
2008\Projects\090325\
090325\Program.cs:riga 28


Looking for this error i've seen that SQLite doesn't permit to load an
extension before enabling it, with a function like
enable_load_extension but i can't find it.
Can anyone help me? Thanks for attention. I post also the simple
actual source code i'm using:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;

namespace test
{
class Program
{

static void Main(string[] args)
{

// Create a connection and a command
using (DbConnection cnn = new SQLiteConnection("Data
Source=test.sqlite"))
using (DbCommand cmd = cnn.CreateCommand())
{

// Open the connection. If the database doesn't exist,
// it will be created automatically
cnn.Open();

cmd.CommandText = "SELECT load_extension
('libspatialite-2.dll');";
cmd.ExecuteNonQuery(); // Run queries here

// Now read them back
cmd.CommandText = "SELECT * FROM testTable";
using (DbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("ID = {0},
value = {1}", reader[0], reader[1]));
}

}

}

Console.ReadKey();

}

}

}



jan

unread,
Mar 26, 2009, 1:52:20 PM3/26/09
to spatiali...@googlegroups.com
Hi,

what do you mean with ", with a function like
> enable_load_extension but i can't find it."?

The function is called sqlite3_enable_load_extension

http://www.sqlite.org/c3ref/enable_load_extension.html

I am not familiar with .NET but with Qt I had to rebuild the driver
without OMIT_EXTENSIONS (or similar, don't remember exactly).

Maybe this is of any use to you.

Cheers,
Jan



Axel schrieb:

FObermaier

unread,
Mar 28, 2009, 7:19:39 AM3/28/09
to SpatiaLite Users
Hi,
I'm successfully loading the spatialite extension.

http://code.google.com/p/sharpmapv2/source/browse/trunk/SharpMap.Data.Providers/SharpMap.Data.Providers.SpatialLite2/SpatiaLite2DbUtility.cs
(look at the CreateConnection(..) function)
Be sure to have all necessary dlls in the same path as your .net
binary or the search path set accordingly.

This works fine with version 1.0.60 of System.Data.SQLite (latest)
from http://sqlite.phxsoftware.com/

Hth
FObermaier
Reply all
Reply to author
Forward
0 new messages