First, this requires the use of ODAC, which is Oracle.DataAccess.dll, the new managed Oracle.ManagedDataAccess.dll does not support object Type and cannot be used
ODAC Reference: http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html
Odac use method Reference: http://blog.csdn.net/rrrrssss00/article/details/7178515 code See attachment
Http://pan.baidu.com/s/1dDquLhv
Or
Http://www.kuaipan.cn/file/id_22823997376823621.htm
The general idea is to construct a corresponding class in C # based on the contents of the Sdo_geometry object, and then use OracleParameter to manipulate the object of the class when reading and writing, to read and write to the database Sdo_geometry object. The class name is Sdogeometry, the main code is as follows (it also uses the custom sdopoint,oraclearraytypefactory and Oraclecustomtypebase classes, the code of which is shown in the corresponding file of the attachment)
[CSharp]View PlainCopy
- [Oraclecustomtypemappingattribute ("Mdsys. Sdo_geometry ")]
- public class sdogeometry:oraclecustomtypebase<sdogeometry>
- {
- private enum Oracleobjectcolumns {sdo_gtype, Sdo_srid, Sdo_point, Sdo_elem_info, sdo_ordinates}
- private decimal? Sdo_gtype;
- [Oracleobjectmappingattribute (0)]
- Public decimal? Sdo_gtype
- {
- get { return sdo_gtype;}
- set {Sdo_gtype = value;}
- }
- private decimal? Sdo_srid;
- [Oracleobjectmappingattribute (1)]
- Public decimal? Sdo_srid
- {
- get { return sdo_srid;}
- set {Sdo_srid = value;}
- }
- private Sdopoint Point;
- [Oracleobjectmappingattribute (2)]
- Public Sdopoint Point
- {
- get { return point;}
- set {point = value;}
- }
- private decimal[] Elemarray;
- [Oracleobjectmappingattribute (3)]
- public decimal[] Elemarray
- {
- get { return elemarray;}
- set {Elemarray = value;}
- }
- private decimal[] Ordinatesarray;
- [Oracleobjectmappingattribute (4)]
- public decimal[] Ordinatesarray
- {
- get { return ordinatesarray;}
- set {Ordinatesarray = value;}
- }
- [Oraclecustomtypemappingattribute ("Mdsys. Sdo_elem_info_array ")]
- public class elemarrayfactory:oraclearraytypefactorybase<decimal> {}
- [Oraclecustomtypemappingattribute ("Mdsys. Sdo_ordinate_array ")]
- public class ordinatesarrayfactory:oraclearraytypefactorybase<decimal> {}
- public override void Mapfromcustomobject ()
- {
- SetValue ((int) oracleobjectcolumns.sdo_gtype, sdo_gtype);
- SetValue ((int) Oracleobjectcolumns.sdo_srid, sdo_srid);
- SetValue ((int) oracleobjectcolumns.sdo_point, point);
- SetValue ((int) oracleobjectcolumns.sdo_elem_info, elemarray);
- SetValue ((int) oracleobjectcolumns.sdo_ordinates, ordinatesarray);
- }
- public override void Maptocustomobject ()
- {
- Sdo_gtype = getvalue<decimal?> ((int) oracleobjectcolumns.sdo_gtype);
- Sdo_srid = getvalue<decimal?> ((int) oracleobjectcolumns.sdo_srid);
- Point = Getvalue<sdopoint> ((int) oracleobjectcolumns.sdo_point);
- Elemarray = getvalue<decimal[]> ((int) oracleobjectcolumns.sdo_elem_info);
- Ordinatesarray = getvalue<decimal[]> ((int) oracleobjectcolumns.sdo_ordinates);
- }
- }
The code read from the database is (the sample table has only two columns, the ID column is the number type, and the Geo column is the Sdo_geomtry type):
[CSharp]View PlainCopy
- OracleCommand cmd = new OracleCommand ()
- Cmd. Connection = con;
- Cmd.commandtype = CommandType.Text;
- Cmd.commandtext = "Select Id,geo from Geoinfo";
- using (oracledatareader readergeoinfo = cmd. ExecuteReader ())
- {
- While (Readergeoinfo.read ())
- {
- Geoinfo geoinfo = new Geoinfo ();
- if (!readergeoinfo.isdbnull (0))
- {
- geoinfo.id = readergeoinfo.getdecimal (0);
- }
- if (!readergeoinfo.isdbnull (1))
- {
- Geoinfo.geo = (sdogeometry) readergeoinfo.getvalue (1);
- }
- Geoinfolist.add (Geoinfo);
- }
- Readergeoinfo.close ();
- }
The inserted code is:
[CSharp]View PlainCopy
- Cmd.commandtext = "INSERT into geoinfo values (Geoinfo_seq.nextval,:p Aram)";
- Cmd. Parameters.clear ();
- OracleParameter Oracleparametergeo = new OracleParameter ();
- Oracleparametergeo.oracledbtype = Oracledbtype.object;
- Oracleparametergeo.udttypename = "Mdsys. Sdo_geometry ";
- Cmd. Parameters.Add (Oracleparametergeo);
- //creating Point
- Sdogeometry geoPoint = new Sdogeometry ();
- Geopoint.sdo_gtype = 2001;
- Geopoint.point = new Sdopoint ();
- Geopoint.point.x = 200;
- Geopoint.point.y = 400;
- Oracleparametergeo.value = GeoPoint;
- //insert Point in table Geoinfo
- Cmd. ExecuteNonQuery ();
- //creating Polygon
- Sdogeometry Geopolygon = new Sdogeometry ();
- Geopolygon.sdo_gtype = 2003;
- Geopolygon.elemarray = new decimal[] {1, 1003, 1};
- Geopolygon.ordinatesarray = new decimal[] {3, 3, 3, 10, 10, 10, 10, 3, 3, 3};
- Oracleparametergeo.value = Geopolygon;
- //insert polygon into table Geoinfo
- Cmd. ExecuteNonQuery ();
In practice, when the query results of SELECT * are put into a DataTable using the DataAdapter Fill method, the query result automatically considers the column of the DataTable to be sdogeometry if the Sdogeometry class is already defined. , very convenient, for example oracledataadapter MADP = new OracleDataAdapter ("SELECT * from Geoinfo", con); DataTable MDST = new DataTable (); Madp.fill (MDST); At this point the second column data type of MDST is Sdogeometry
Using C # to manipulate Sdo_geometry (read and write) for Oracle spatial