Dim cnn As New OracleConnection( _
"user id=scott;password=tiger;data source=orcl")
cnn.Open()
Dim cmd As OracleCommand = New OracleCommand("", cnn)
cmd.CommandText = _
"SELECT empno, ename, sal, " & _ "deptno FROM emp ORDER BY empno DESC"
cmd.XmlCommandType = OracleXmlCommandType.Query
Dim empReader As XmlReader = cmd.ExecuteXmlReader()
Dim empDoc As XmlDocument = New XmlDocument
empDoc.PreserveWhitespace = True
empDoc.Load(empReader)
Dim strXmlDoc As String = empDoc.OuterXml
CREATE OR REPLACE VIEW SCOTT.EMP_VIEW
(SYS_NC_ROWINFO$)
AS
SELECT XMLELEMENT("EMP",
XMLFOREST(empno, ename, job,sal,deptno)
) FROM emp
リスト13 emp表をXML文書に変換するビューの作成
XML文書に変換されたビューをOracleXmlTypeに取得するコードは以下のようになります。
Dim cnn As New OracleConnection( _
"user id=scott;password=tiger;data source=orcl")
cnn.Open()
Dim cmd As OracleCommand = New OracleCommand("", cnn)
Dim empXml As OracleXmlType
cmd.CommandText = "SELECT * FROM emp_view t " & _
"ORDER BY EXTRACTVALUE(VALUE(t), '/EMP/EMPNO/text()') DESC"
cmd.Connection = cnn
Dim empReader As OracleDataReader = cmd.ExecuteReader()
Dim str As String = ""
While (empReader.Read())
empXml = empReader.GetOracleXmlType(0)
' XMLの操作……
End While