Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set InsertCommand parameter as function call?

I working on a GIS application which uses the PostgreSQL database with PostGIS extension. I already created the data access logic for entire project and everything works fine except the geometry value insert.

I have to pass geometry value as a function call:

INSERT INTO mygeotable (id, name, geom) VALUES 
    (1, "MyName", ST_GeomFromText('POINT(755888.4156 112458.556)', 23700))

As you can see I have to call the ST_GeomFromText PostGIS function on INSERT for the geom column value. This geom column is a string value from the view of ADO.NET but it can't be set as a text on INSERT.

I think ADO.NET automatically formats my INSERT command this way:

INSERT INTO mygeotable (id, name, geom) VALUES 
    (1, "MyName", "ST_GeomFromText('POINT(755888.4156 112458.556)', 23700)")

Which is wrong because of quotation marks!

How can I set INSERT command's geom parameter to use ST_GeomFromText function call?

I use parameters for every column. InsertCommand CommandText is:

INSERT INTO mygeotable (id, name, geom) VALUES (id, name, geom)

The geom paramter DbType is String. There is no DbType for function call!

like image 574
ggabor Avatar asked Jan 17 '26 21:01

ggabor


1 Answers

Pass the parameters to the function as numbers in instead of passing the whole function call:

INSERT INTO mygeotable (id, name, geom) VALUES
    (id, name, ST_GeomFromText('POINT(X Y)', Z))

BTW double quotes like in "MyName" do not make valid strings as they are reserved for identifiers. Strings should be wrapped by single quotes.

like image 59
Clodoaldo Neto Avatar answered Jan 20 '26 12:01

Clodoaldo Neto