Using C # to manipulate Sdo_geometry (read and write) for Oracle spatial

Source: Internet
Author: User

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  
  1. [Oraclecustomtypemappingattribute ("Mdsys. Sdo_geometry ")]
  2. public class sdogeometry:oraclecustomtypebase<sdogeometry>
  3. {
  4. private enum Oracleobjectcolumns {sdo_gtype, Sdo_srid, Sdo_point, Sdo_elem_info, sdo_ordinates}
  5. private decimal? Sdo_gtype;
  6. [Oracleobjectmappingattribute (0)]
  7. Public decimal? Sdo_gtype
  8. {
  9. get { return sdo_gtype;}
  10. set {Sdo_gtype = value;}
  11. }
  12. private decimal? Sdo_srid;
  13. [Oracleobjectmappingattribute (1)]
  14. Public decimal? Sdo_srid
  15. {
  16. get { return sdo_srid;}
  17. set {Sdo_srid = value;}
  18. }
  19. private Sdopoint Point;
  20. [Oracleobjectmappingattribute (2)]
  21. Public Sdopoint Point
  22. {
  23. get { return point;}
  24. set {point = value;}
  25. }
  26. private decimal[] Elemarray;
  27. [Oracleobjectmappingattribute (3)]
  28. public decimal[] Elemarray
  29. {
  30. get { return elemarray;}
  31. set {Elemarray = value;}
  32. }
  33. private decimal[] Ordinatesarray;
  34. [Oracleobjectmappingattribute (4)]
  35. public decimal[] Ordinatesarray
  36. {
  37. get { return ordinatesarray;}
  38. set {Ordinatesarray = value;}
  39. }
  40. [Oraclecustomtypemappingattribute ("Mdsys. Sdo_elem_info_array ")]
  41. public class elemarrayfactory:oraclearraytypefactorybase<decimal> {}
  42. [Oraclecustomtypemappingattribute ("Mdsys. Sdo_ordinate_array ")]
  43. public class ordinatesarrayfactory:oraclearraytypefactorybase<decimal> {}
  44. public override void Mapfromcustomobject ()
  45. {
  46. SetValue ((int) oracleobjectcolumns.sdo_gtype, sdo_gtype);
  47. SetValue ((int) Oracleobjectcolumns.sdo_srid, sdo_srid);
  48. SetValue ((int) oracleobjectcolumns.sdo_point, point);
  49. SetValue ((int) oracleobjectcolumns.sdo_elem_info, elemarray);
  50. SetValue ((int) oracleobjectcolumns.sdo_ordinates, ordinatesarray);
  51. }
  52. public override void Maptocustomobject ()
  53. {
  54. Sdo_gtype = getvalue<decimal?> ((int) oracleobjectcolumns.sdo_gtype);
  55. Sdo_srid = getvalue<decimal?> ((int) oracleobjectcolumns.sdo_srid);
  56. Point = Getvalue<sdopoint> ((int) oracleobjectcolumns.sdo_point);
  57. Elemarray = getvalue<decimal[]> ((int) oracleobjectcolumns.sdo_elem_info);
  58. Ordinatesarray = getvalue<decimal[]> ((int) oracleobjectcolumns.sdo_ordinates);
  59. }
  60. }


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  
  1. OracleCommand cmd = new OracleCommand ()
  2. Cmd. Connection = con;
  3. Cmd.commandtype = CommandType.Text;
  4. Cmd.commandtext = "Select Id,geo from Geoinfo";
  5. using (oracledatareader readergeoinfo = cmd. ExecuteReader ())
  6. {
  7. While (Readergeoinfo.read ())
  8. {
  9. Geoinfo geoinfo = new Geoinfo ();
  10. if (!readergeoinfo.isdbnull (0))
  11. {
  12. geoinfo.id = readergeoinfo.getdecimal (0);
  13. }
  14. if (!readergeoinfo.isdbnull (1))
  15. {
  16. Geoinfo.geo = (sdogeometry) readergeoinfo.getvalue (1);
  17. }
  18. Geoinfolist.add (Geoinfo);
  19. }
  20. Readergeoinfo.close ();
  21. }


The inserted code is: [CSharp]View PlainCopy  
  1. Cmd.commandtext = "INSERT into geoinfo values (Geoinfo_seq.nextval,:p Aram)";
  2. Cmd. Parameters.clear ();
  3. OracleParameter Oracleparametergeo = new OracleParameter ();
  4. Oracleparametergeo.oracledbtype = Oracledbtype.object;
  5. Oracleparametergeo.udttypename = "Mdsys.  Sdo_geometry ";
  6. Cmd. Parameters.Add (Oracleparametergeo);
  7. //creating Point
  8. Sdogeometry geoPoint = new Sdogeometry ();
  9. Geopoint.sdo_gtype = 2001;
  10. Geopoint.point = new Sdopoint ();
  11. Geopoint.point.x = 200;
  12. Geopoint.point.y = 400;
  13. Oracleparametergeo.value = GeoPoint;
  14. //insert Point in table Geoinfo
  15. Cmd. ExecuteNonQuery ();
  16. //creating Polygon
  17. Sdogeometry Geopolygon = new Sdogeometry ();
  18. Geopolygon.sdo_gtype = 2003;
  19. Geopolygon.elemarray = new decimal[] {1, 1003, 1};
  20. Geopolygon.ordinatesarray = new decimal[] {3, 3, 3, 10, 10, 10, 10, 3, 3, 3};
  21. Oracleparametergeo.value = Geopolygon;
  22. //insert polygon into table Geoinfo
  23. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.