访问Oracle Spatial中的SDO_GEOMETRY字段的方法(windows下)

1.使用oci或者occi,在安装oracle后可以在oraclehome\md\demo\examples目录下参考其C++实现,
该方法效率最高,但实现比较复杂;

2.使用Oracle提供的OO4O,是com,在vb6代码如下:
‘***************************************************************
‘Gisc Lab, Nanjing Normal University

‘Purpose: This Demo Shows how to read spatial data from Oracle

‘Input: User ID=’Scott’, Password=’Tiger’, Data Source=’Apollo’,
‘ SQL=’select * from interstates where rownum<2’

‘Output: xmlDocument or xmlReader

‘Author: Charlie Chen

‘Data: 7-28-2004
‘***************************************************************

Dim OraSession As OraSessionClass
Dim OraDatabase As OraDatabase
Dim interstates As OraDynaset
Dim geom As OraObject
Dim coords As OraCollection

Private Sub Command1_Click()
Set OraSession = New OraSessionClass

Set OraDatabase = OraSession.OpenDatabase(“apollo”, “scott/tiger”, 0&)

Set interstates = OraDatabase.CreateDynaset(“select * from interstates where rownum<2”, 0&)

Set geom = interstates.Fields(“GEOM”).Value

Set coords = geom.SDO_ORDINATES

For i = 1 To coords.Size
Debug.Print coords.Item(i)
Next i
End Sub

程序可读性和维护性较好,需要添加对oip9.tlb的引用

3.使用ODP.NET(Oracle公司提供的在.net平台上的数据访问组件),在.NET环境下可以直接转换为xml坐标字符串,但效率太低;

4.使用JDBC或者SDO API,无论从哪方面来讲都比较理想。

补充.net平台上的实现,代码如下:

    private XmlDocument GetSpatialData(string connectionString, string SQL)
{
OracleConnection con = new OracleConnection(connectionString);

try
{
con.Open();

OracleCommand cmd = new OracleCommand(SQL, con);
cmd.XmlCommandType = OracleXmlCommandType.Query;
cmd.BindByName = true;
int rows = cmd.ExecuteNonQuery();

XmlReader xmlReader = cmd.ExecuteXmlReader();

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.Load(xmlReader);

return xmlDocument;
}
catch(Exception e)
{
throw e;
}
finally
{
if (con.State == System.Data.ConnectionState.Open) con.Close();
con.Dispose();
}
}

    调用:
    string constr = “User Id=scott;Password=tiger;Data Source=apollo”;
string sql = “select * from interstates where rownum<2”;

XmlDocument xmlDocument = GetSpatialData(constr, sql);

Console.WriteLine((xmlDocument != null) ? xmlDocument.InnerXml : “fail to read”);

 

转载自:https://blog.csdn.net/iihero/article/details/83081674

You may also like...