Dim cnn As New OracleConnection( _
"user id=scott;password=tiger;data source=orcl")
cnn.Open()
Dim sb As StringBuilder = New StringBuilder
sb.Append("<EMP>")
sb.Append("<EMPNO>7370</EMPNO>")
sb.Append("<ENAME>田中太郎</ENAME>")
sb.Append("<JOB>社員</JOB>")
sb.Append("<HIREDATE>1998-02-14</HIREDATE>")
sb.Append("<SAL>300000</SAL>")
sb.Append("<DEPTNO>10</DEPTNO>")
sb.Append("</EMP>")
Dim xml As OracleXmlType = _
New OracleXmlType(cnn, sb.ToString())
Dim cmd As OracleCommand = New OracleCommand("", cnn)
cmd.CommandText = "INSERT INTO EMP_XMLDB VALUES (:1)"
cmd.CommandType = CommandType.Text
Dim p1 As OracleXmlType = _
New OracleXmlType(cnn, sb.ToString())
cmd.Parameters.Add(":1", OracleDbType.XmlType, p1, _
ParameterDirection.Input)
cmd.ExecuteNonQuery()
リスト1 XMLTypeにXML文書をパラメータとして渡すコード(VB.NET)
OracleConnection cnn = new OracleConnection(
"user id=scott;password=tiger;data source=orcl");
cnn.Open();
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 * FROM EMP_XMLDB"
Dim rdr As OracleDataReader = cmd.ExecuteReader()
Dim EmpXML As OracleXmlType
If rdr.Read() Then
EmpXML = rdr.GetOracleXmlType(0)
' XMLの処理……
End If
リスト3 XMLTypeからXML文書を取得するコード(VB.NET)
OracleConnection cnn = new OracleConnection(
"user id=scott;password=tiger;data source=orcl");
cnn.Open();
OracleCommand cmd = new OracleCommand("", cnn);
cmd.CommandText = "SELECT * FROM EMP_XMLDB";
OracleDataReader rdr = cmd.ExecuteReader();
OracleXmlType EmpXML;
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 * FROM EMP_XMLDB"
Dim rdr As OracleDataReader = cmd.ExecuteReader()
Dim EmpXML As OracleXmlType
If rdr.Read() Then
EmpXML = rdr.GetOracleXmlType(0)
EmpXML.Update("/EMP/ENAME/text()", String.Empty, _
"鈴木太郎")
cmd.CommandText = _
"Update EMP_XMLDB Set SYS_NC_ROWINFO$=:1"
cmd.Parameters.Add(":1", OracleDbType.XmlType, EmpXML, _
ParameterDirection.Input)
cmd.ExecuteNonQuery()
End If
リスト5 OracleXmlTypeを操作してXML文書を更新するコード(VB.NET)
OracleConnection cnn = new OracleConnection(
"user id=scott;password=tiger;data source=orcl");
cnn.Open();
OracleCommand cmd = new OracleCommand("", cnn);
cmd.CommandText = "SELECT * FROM EMP_XMLDB";
OracleDataReader rdr = cmd.ExecuteReader();
OracleXmlType EmpXML;
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 * FROM EMP_XMLDB"
Dim rdr As OracleDataReader = cmd.ExecuteReader()
Dim EmpXML As OracleXmlType
If rdr.Read() Then
EmpXML = rdr.GetOracleXmlType(0)
Dim xdoc As XmlDocument = EmpXML.GetXmlDocument()
xdoc.SelectSingleNode("/EMP/ENAME").InnerText = "鈴木太郎"
Dim EmpXMLUpd As New OracleXmlType(cnn, xdoc)
cmd.CommandText = "Update EMP_XMLDB Set SYS_NC_ROWINFO$=:1"
cmd.Parameters.Add(":1", OracleDbType.XmlType, EmpXMLUpd, _
ParameterDirection.Input)
cmd.ExecuteNonQuery()
End If