Hello again,
In the following I will show my first two solutions (one which
re-creates the SQLiteCommand for every record and one throwing
exceptions) and the working solution and I will explain, what I think I
did wrong.
Also I will show the performance differences.
Problem summary:
My dataset consists of about 10.000.000 records and each record has < 10
vertices.
Solution 1:
===============================================================
// ... code ...
foreach(var polyline in Polylines)
{
using(var cmd = _connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO Polylines (Id, Geometry)
VALUES(" + polyline.Id + ", ST_GeomFromText('" + Polyline2WKT(polyline)
+ "', 3857))";
cmd.ExecuteNonQuery();
}
}
/*
Converts a polyline to the Well-Known Text format, e.g. "LINESTRING(1 1,
2 2, 3 3)"
*/
private static string Polyline2WKT(Polyline Polyline){ /* ... code ... */}
// ... code ...
===============================================================
This solution takes about 45 minutes for the dataset.
Solution 2 (not working):
===============================================================
// ... code ...
using(var cmd = _connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO Polylines (Id, Geometry)
VALUES(@IdParam, @GeometryParam)"; // 0
var idParam = new SQLiteParameter("@IdParam");
cmd.Parameters.Add(idParam);
var geometryParam = new SQLiteParameter("@GeometryParam");
cmd.Parameters.Add(geometryParam);
foreach(var polyline in Polylines)
{
idParam.Value = polyline.Id;
geometryParam.Value = "ST_GeomFromText('" +
Polyline2WKT(polyline) + "', 3857)"; // 1
cmd.ExecuteNonQuery();
}
}
/*
Converts a polyline to the Well-Known Text format, e.g. "LINESTRING(1 1,
2 2, 3 3)"
*/
private static string Polyline2WKT(Polyline Polyline){ /* ... code ... */}
// ... code ...
===============================================================
This "solution" throws exceptions. This is (at least I think so) because
I used the spatialite function "ST_GeomFromText" in the command
parameter and not in the CommandText (see comments 0 and 1).
Initially i thought the error appears because I did not set the
appropriate DbType's for the SQLiteParameter (I simply overlooked the
spatialite function).
That was the reason I asked about Extension datatypes for spatialite.
Thanks to the conversation with Sandro I noticed the mistake and came up
with a workin solution which is presented in the following:
Solution 3:
===============================================================
// ... code ...
using(var cmd = _connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO Polylines (Id, Geometry)
VALUES(@IdParam, ST_GeomFromText(@GeometryParam, 3857))"; // 0
var idParam = new SQLiteParameter("@IdParam");
cmd.Parameters.Add(idParam);
var geometryParam = new SQLiteParameter("@GeometryParam");
cmd.Parameters.Add(geometryParam);
foreach(var polyline in Polylines)
{
idParam.Value = polyline.Id;
geometryParam.Value = Polyline2WKT(polyline); // 1
cmd.ExecuteNonQuery();
}
}
/*
Converts a polyline to the Well-Known Text format, e.g. "LINESTRING(1 1,
2 2, 3 3)"
*/
private static string Polyline2WKT(Polyline Polyline){ /* ... code ... */}
// ... code ...
===============================================================
See the differences to Solution 2 at the comments 0 and 1.
This solution takes about 15 minutes for the dataset.