扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
CONNECT TO AJAXDEMO; ------------------------------------------------ -- DDL Statements for table "NULLID "."HEROES_NOXML" ------------------------------------------------ CREATE TABLE "NULLID "."HEROES_NOXML" ( "HEROID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +1 INCREMENT BY +1 MINVALUE +1 MAXVALUE +2147483647 NO CYCLE NO CACHE NO ORDER ) , "HERONAME" VARCHAR(50) NOT NULL , "POWER" VARCHAR(255) , "WEAKNESS" VARCHAR(255) , "NOTES" CLOB(1048576) LOGGED NOT COMPACT ) IN "USERSPACE1" ; -- DDL Statements for primary key on Table "NULLID "."HEROES_NOXML" ALTER TABLE "NULLID "."HEROES_NOXML" ADD CONSTRAINT "CC1158547603783" PRIMARY KEY ("HEROID"); ------------------------------------------------ -- DDL Statements for table "NULLID "."HEROES_XML" ------------------------------------------------ CREATE TABLE "NULLID "."HEROES_XML" ( "HEROID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +1 INCREMENT BY +1 MINVALUE +1 MAXVALUE +2147483647 NO CYCLE NO CACHE NO ORDER ) , "HERONAME" VARCHAR(50) NOT NULL , "NOTES" XML ) IN "USERSPACE1" ; -- DDL Statements for primary key on Table "NULLID "."HEROES_XML" ALTER TABLE "NULLID "."HEROES_XML" ADD CONSTRAINT "CC1158547736768" PRIMARY KEY ("HEROID"); COMMIT WORK; CONNECT RESET; TERMINATE; This creates the following two tables: HEROES_NOXML: HEROID, INTEGER (4), IDENTITY, NOT NULLABLE HERONAME, VARCHAR (50), NOT NULLABLE POWER, VARCHAR (255), NULLABLE WEAKNESS, VARCHAR (255), NULLABLE NOTES, CLOB, NULLABLE HEROES_XML: HEROID, INTEGER (4), IDENTITY, NOT NULLABLE HERONAME, VARCHAR (50), NOT NULLABLE NOTES, XML, NULLABLE Populate the first table by copying the SQL code from Listing 2 into your Command Editor. Listing 2: HEROES_NOXML Data INSERT INTO NULLID.HEROES_NOXML (HERONAME, POWER, NOTES) VALUES ('Mockingbird','Can mimic any sound she hears.','Alter ego: Mariah Carey'); INSERT INTO NULLID.HEROES_NOXML (HERONAME, POWER, WEAKNESS, NOTES) VALUES ('Indigo','Telepathy','Gets cranky if he stays up too late.','Current age: 8. Additional weakness: lactose intolerant.'); INSERT INTO NULLID.HEROES_NOXML (HERONAME, POWER, WEAKNESS, NOTES) VALUES ('Indivisibull','Super strength; can reattach severed body parts.','Not too bright.','Secret origin: a somewhat clumsy man, Hank "Bullhead" Durham fell into a vat of fresh glue and was promptly struck by lightning. Since then, whenever he loses a limb, he has simply to hold it in place and the limb becomes reattached.'); INSERT INTO NULLID.HEROES_NOXML (HERONAME, POWER, WEAKNESS, NOTES) VALUES ('Dayglo','Glows under a black light.','Not having a black light around.','Usually relegated to sidekick, though once played a crucial role in a mission busting an underground pot growers cabal.'); INSERT INTO NULLID.HEROES_NOXML (HERONAME, POWER, WEAKNESS, NOTES) VALUES ('Thumbalina','Ability to text message at light speed.','Thumbsucking (a nervous habit in times of great stress).','Former codename: Twiddle.'); |
using System; using System.Data; using System.Web.Services; using System.Web.Services.Protocols; using System.Web; using IBM.Data.DB2; using System.Data.Common; [WebService()] [WebServiceBinding()] public class HeroData { [WebMethod()] public virtual DataSet select() { IBM.Data.DB2.DB2Connection db2Connection1 = new IBM.Data.DB2.DB2Connection(); IBM.Data.DB2.DB2DataAdapter db2DataAdapter1 = new IBM.Data.DB2.DB2DataAdapter(); IBM.Data.DB2.DB2Command db2SelectCommand1 = new IBM.Data.DB2.DB2Command(); db2Connection1.ConnectionString = "database=AJAXDEMO;user id=db2admin;server=DELL:db2c_DB2;password=changeme"; db2DataAdapter1.SelectCommand = db2SelectCommand1; db2DataAdapter1.SelectCommand.CommandType = CommandType.Text; db2DataAdapter1.SelectCommand.CommandText =@"select * from NULLID.HEROES_NOXML"; db2DataAdapter1.SelectCommand.Connection = db2Connection1; System.Data.DataSet ds = new System.Data.DataSet(); try { db2DataAdapter1.Fill(ds); } catch (DB2Exception ex) { throw ex; } finally { db2Connection1.Close(); } return ds; } } |
XmlDataDocument xd = new XmlDataDocument(ds); return xd; |
<HERO> <NOTE title="Power">Can mimic any sound she hears.</NOTE> <NOTE title="Alter ego">Mariah Carey</NOTE> </HERO> Listing 3: hero2.xml <HERO> <NOTE title="Power">Telepathy</NOTE> <NOTE title="Weakness">Gets cranky if he stays up too late.</NOTE> <NOTE title="Current age">8</NOTE> <NOTE title="Additional weakness">Lactose intolerant.</NOTE> </HERO> Listing 4: hero3.xml <HERO> <NOTE title="Power">Super strength; can reattach severed body parts.</NOTE> <NOTE title="Weakness">Not too bright.</NOTE> <NOTE title="Secret Origin">A somewhat clumsy man, Hank "Bullhead" Durham fell into a vat of fresh glue and was promptly struck by lightning. Since then, whenever he loses a part of his body, he has simply to hold it in place and it becomes reattached.</NOTE> </HERO> Listing 5: hero4.xml <HERO> <NOTE title="Power">Glows under a black light.</NOTE> <NOTE title="Weakness">Not having a black light around.</NOTE> <NOTE title="Note">Usually relegated to sidekick, though once played a crucial role in a mission busting an underground pot growers cabal.</NOTE> </HERO> Listing 6: hero5.xml <HERO> <NOTE title="Power">Ability to text message at light speed.</NOTE> <NOTE title="Weakness">Thumbsucking (a nervous habit in times of great stress).</NOTE> <NOTE title="Former Codename">Twiddle</NOTE> </HERO> |
Mockingbird,<XDS FIL='hero1.xml' /> Indigo,<XDS FIL='hero2.xml' /> Indivisibull,<XDS FIL='hero3.xml' /> Dayglo,<XDS FIL='hero4.xml' /> Thumbalina,<XDS FIL='hero5.xml' /> |
IMPORT FROM 'C:\PALS\heroes.del' OF DEL XML FROM 'C:\PALS' MODIFIED BY IDENTITYMISSING INSERT INTO NULLID.HEROES_XML |
[WebMethod()] public virtual XmlDocument getWithXML(int heroID) { DB2Connection conn = new DB2Connection(); conn.ConnectionString = connString; conn.Open(); DB2Command cmd = conn.CreateCommand(); XmlDocument heroXml = new XmlDocument(); String sel = @"select XMLELEMENT (NAME ""HERODATA"", XMLELEMENT (NAME ""HeroName"", HERONAME), XMLELEMENT (NAME ""Notes"", NOTES)) from NULLID.HEROES_XML where HEROID = " + heroID.ToString() + ";"; cmd.CommandText = sel; XmlReader xr = cmd.ExecuteXmlReader(); try { heroXml.Load(xr); } catch (DB2Exception ex) { throw ex; } finally { conn.Close(); } return heroXml; } |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>DB2 AJAX Sample</title> </head> <body> <form id="form1" runat="server"> <div id="heroList"> </div> <div id="notes">Choose a Hero</div> </form> </body> </html> |
<HeaderTemplate> <h3>Available Roster:</h3> <ul class="HeroList"> </HeaderTemplate> |
<ItemTemplate> <li class="heroItem" onclick="getHeroInfo(<%# DataBinder.Eval(Container.DataItem, "HeroID")%>)" onmouseover="this.className='heroItem_hl';" onmouseout="this.className='heroItem';"> <%# DataBinder.Eval(Container.DataItem, "HeroName")%> </li> </ItemTemplate> |
<FooterTemplate> </ul> </FooterTemplate> |
<script language="javascript" type="text/javascript"> var request; function createRequest() { try { request = new XMLHttpRequest(); } catch (tryms) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (otherms) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = null; } } } if (request == null) alert("Error implementing Ajax functionality"); } function getHeroInfo(heroID) { createRequest(); var url = "http://localhost/Superpals/HeroData.asmx/getWithXML"; request.open("POST", url, true); request.onreadystatechange = showHeroInfo; request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send("heroID=" + heroID); } function showHeroInfo() { if (request.readyState == 4) { if (request.status == 200) { var xmlDoc = request.responseXML.documentElement; var notesList = xmlDoc.getElementsByTagName("HERO")[0].getElementsByTagName("NOTE"); var len = notesList.length; var notesDiv = document.getElementById("notes"); //Clear Notes Span tag while (notesDiv.firstChild) { notesDiv.removeChild(notesDiv.firstChild); } var newSpan, newText; for (var n = 0; n<len; n++) { var newSpan = document.createElement("h3"); newSpan.className = "noteTitle"; var newTextVal = notesList[n].getAttributeNode("title").nodeValue + ": "; var newText = document.createTextNode(newTextVal)newSpan.appendChild(newText); notesDiv.appendChild(newSpan); var newSpan = document.createElement("p"); newSpan.className = "noteValue"; var newTextVal = notesList[n].firstChild.nodeValue; var newText = document.createTextNode(newTextVal)newSpan.appendChild(newText); notesDiv.appendChild(newSpan); } } else { var stat = request.getResponseHeader("Status"); alert(request.status + " | " + stat); } } } </script> |
var notesList = xmlDoc.getElementsByTagName("HERO")[0].getElementsByTagName("NOTE"); |
<style type="text/css"> ul {font-family:Arial; font-size:.9em; } li { list-style:none; font-family:Arial; } h3 { font-family:Arial; font-size:1.3em} p { font-family:Arial; font-weight:normal; font-size:.9em} #notes { float:right; border-width:1px; border-style:solid; width:50%; padding:20px 20px 20px 20px;} #heroList { font-family:Arial; float:left; width:30%; padding:20px 20px 20px 20px;} .heroItem_hl { font-weight:bold; background-color:azure; } .heroItem { font-weight:normal; background-color:white; } </style> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。