Databases Reference
In-Depth Information
Console.WriteLine("\nRows Updated: \n"+rowsUpdated);
conn.Close();
return rowsUpdated;
}
If we need to update customer information using the name of the customer, we
need to first obtain the customer ID (CID). The CID is an attribute of customerinfo
element in the info XML data, which needs to be queried using the customer
name.
The GetCustomerID() method shown in Example 6-15 will demonstrate how
XMLQuery can be used to obtain the CID attribute using the name element.
The XMLQuery that will yield CID using the customer name is shown in
Example 6-14.
Example 6-14 XMLQuery yielding CID
SELECT XMLCAST(xmlquery('declare default element namespace
"http://posample.org";$d/customerinfo/@Cid' passing customer.info as
"d" ) AS int) FROM customer where xmlexists('declare default element
namespace "http://posample.org" ;$d/customerifo[name= "Customer Name"
]' passing customer.info as "d")
Example 6-15 GetCustomerID method which will retrieve CID using XMLQuery
public static void GetCustomerID(DB2Connection conn, String pname)
{
int cid=0;
String xmlquerystmt= "SELECT XMLCAST(xmlquery('declare default
element namespace "+
"\"http://posample.org\";$d/customerinfo/@Cid' passing customer.info
as \"d\")"+
"AS int) FROM customer where xmlexists('declare default element
namespace "+
"\"http://posample.org\";$d/customerinfo[name=\""+pname+"\"]'
passing customer.info as \"d\");";
Console.WriteLine("XMLQuery varchar(10) : "+xmlquerystmt);
DB2Command cmd = new DB2Command(xmlquerystmt, conn);
DB2DataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
//Read in the int value and assign to cid
cid=reader.GetInt32(0);
Search WWH ::




Custom Search