麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

XML 與 XML DB 和 ODP.NET 結合使用

2024-07-21 02:33:11
字體:
來源:轉載
供稿:網友

  作者:Mark A. Williams了解如何將 xml 與 Oracle 數據庫、Oracle XML DB 和 Oracle Data PRovider for .NET 結合使用。
許多開發人員已經意識到,并且更多的開發人員正開始意識到 Oracle 數據庫與 .NET 應用程序結合將形成了一個強大的組合。Oracle 數據庫眾多強大特性之一就是能夠使用 XML。利用 Oracle Data Provider for .NET (ODP.NET) 和 Oracle XML DB,通過 Visual Studio.NET 應用程序在 Oracle 數據庫中使用 XML 數據就和使用傳統數據類型一樣輕易。(關于使用 Oracle 進行一般的 .NET 開發的介紹,請參見 John Paul Cook 的文章“在 Oracle 數據庫上構建 .NET 應用程序”。關于使用 XML 和 Oracle Data Provider for .NET. 的其他示例,請參見“ODP.NET 的 XML 支持”頁面。) 本文介紹了利用 ODP.NET 和 Visual Studio .NET 在 Oracle 數據庫中使用 XML 數據的基礎知識,并非凡介紹了
  • 將 XML 模式加載到數據庫中
  • 將 XML 實例文檔加載到數據庫中
  • 創建在 XML 數據上執行 DML 操作的 .NET 應用程序。
為了成功實施本文中的代碼,您需要能夠訪問安裝了 XML DB 的 Oracle9i Release 2 或更高版本的數據庫、Visual Studio.NET、Oracle 10g 客戶端軟件和 ODP.NET 10g。(Oracle 數據庫 10g 下載包括 ODP.NET。) 在本文中,我創建了一個名為“OTNXML”的數據庫用戶來演示 XML 數據的使用。假如您沒有足夠的權限在數據庫中創建擁有相應權限的用戶,那么您需要與您的數據庫治理員協作來完成這個任務。我將本文分解為一系列易于理解的步驟,并從創建數據庫用戶開始。 注重:創建新用戶不是嚴格必需的 — 假如您已經有了一個擁有與第 1 步中列出的權限(或更高權限)的用戶,那么您可以在本文中可以使用該用戶。當然,您也可以為現有用增加缺少的權限。例如,OE 用戶(Oracle 示例模式的一部分)擁有除 create any Directorydrop any directory 權限外的所有必要權限。第 1 步:創建數據庫用戶 為了為本文創建數據庫用戶,請使用 SQL*Plus 以治理用戶(擁有 DBA 角色的用戶)身份登錄數據庫,并執行以下語句: -- create the database user-- adjust tablespace names as necessary for your environmentcreate user otnxml identified by demodefault tablespace userstemporary tablespace tempquota unlimited on users;一旦創建了用戶,則授予其相應的權限和角色。我選擇了僅授予必要的系統權限,而不是授予“傳統的”connectresource 角色。 -- grant system privileges and roles to the usergrant create session,alter session,create table,create trigger,create type,create any directory,drop any directory,xdbadminto otnxml;雖然這些權限大部分您都應當很熟悉,以下摘要用來強調它們的目的和指出它們在本文中的用途:
  • create session— 該權限答應用戶創建與數據庫的會話。沒有該權限,用戶將無法連接(或登錄)數據庫。
  • alter session— 該權限答應用戶改變與數據庫會話相關的屬性。這是成功執行 registerSchema 過程的必要權限。
  • create table— 該權限答應用戶在數據庫中創建表。
  • create trigger— 該權限答應用戶在其擁有的表上創建觸發器。
  • create type— 該權限答應用戶在數據庫中創建新類型。XML 元素將利用類型保存在數據庫中。
  • create any directory— 該權限答應用戶創建目錄對象。目錄對象將用來將 XML 實例文檔加載到數據庫中。這是一種非常強大的權限,應當僅授予可信任的用戶。因為目錄對象存在于數據庫內部的系統命名空間中,因此必須使用“any”要害字。沒有用戶級的“create directory”權限。
  • drop any directory— 該權限是與 create any directory 權限相對應的權限。它答應用戶刪除目錄對象。與 create any directory 權限類似,由于其系統級屬性,該權限應當僅授予可信任的用戶。
  • xdbadmin role — 該權限答應用戶在 XML DB 引擎中創建目錄

注重:由于 create any directorydrop any directory 權限提升的權力,您可能希望在完成本文之后撤銷這些權限(假如您不刪除用戶的話)。您可能還希望刪除您將創建的目錄對象。 第 2 步:創建 XML 模式 因為 XML 模式文檔是基于文本的文檔,因此您可以使用任何文本編輯器來創建它。還有各種工具可以圖形化地創建和處理該模式文檔。然而,為了使本文集中筆墨介紹通過 .NET 在 Oracle 數據庫內部使用 XML,而非介紹 XML 編輯和治理工具,我使用了 Notepad 來創建下面這個簡單的模式文件,我將其命名為“Player.xsd”。該模式和相關的數據代表一個簡單的“player”實體,例如足球隊的一名運動員。 <?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xdb="http://xmlns.oracle.com/xdb"xdb:storeVarrayAsTable="true"version="1.0" ><xsd:element name="Player" xdb:defaultTable="PLAYER"><xsd:complexType xdb:SQLType="PLAYER_T"><xsd:sequence><xsd:element name="PlayerName" type="xsd:string" minOccurs="1"xdb:SQLName="PLAYER_NAME" /><xsd:element name="PlayerPosition" type="xsd:string" minOccurs="1"xdb:SQLName="PLAYER_POSITION" /><xsd:element name="PlayerNumber" type="xsd:positiveInteger"minOccurs="1" xdb:SQLName="PLAYER_NUMBER" /><xsd:element name="PlayerClub" type="xsd:string" minOccurs="1"xdb:SQLName="PLAYER_CLUB" /></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>XML 模式文檔是基本的模式文檔,可能有一點例外:它包含 Oracle XML DB 批注。批注是 World Wide Web Consortium (W3C) XML 模式建議的一部分,它們支持將供給商特定的信息添加到模式定義中。批注還答應覆蓋 XML 類型和 SQL 類型之間的默認映射。這是批注在本模式文檔中的主要用途。為了使用 XML DB 批注,必須在文檔的根元素下指定命名空間。這由以下文本(摘自模式文檔)來實現: xmlns:xdb="http://xmlns.oracle.com/xdb"正如您看到的那樣,我決定了使用文本“xdb”作為整個模式文檔命名空間的縮寫標識符。為了將集合存儲為嵌套表,將以下文本包含在文檔根元素中。 xdb:storeVarrayAsTable="true"為了指定在數據庫內部用來保存數據的表的名稱,我在根元素定義中包含了以下文本: xdb:defaultTable="PLAYER"注重:默認表由 Oracle 創建,并且必須不能是現有表的名稱。例如,您不能“預先創建”該表并告訴 Oracle 將數據加載到其中。 該批注指示數據庫中的 XML DB 引擎:用來存儲該模式所提供數據的表的名稱應為“PLAYER”。其余的批注在名稱和功能方面非常簡單。“SQLName”批注指示表中的列名,“SQLType”批注相應地指示列數據類型或數據庫類型對象。例如,以下文本指示,XML 復雜類型應當以名為“PLAYER_T”的數據庫類型存儲在數據庫中: xdb:SQLType="PLAYER_T"第 3 步:注冊 XML 模式文檔 為了在數據庫中的 XML DB 中注冊 XML 模式文檔,請將“Player.xsd”文件保存在一個您有讀/寫權限的文件夾中。用 XML DB 將文件加載到數據庫中的方式有多種。我將利用一個目錄對象和 DBMS_XMLSCHEMA PL/SQL 程序包來演示該過程。 注重:該目錄對象必須代表一個數據庫可訪問的操作系統目錄。
  1. 以 OTNXML 用戶身份連接數據庫,創建一個目錄對象,該目錄對象將代表 XML 模式和實例文檔所在的操作系統目錄。 -- create the directory object that represents the directory-- where the schema and instance documents residecreate directory OTNXML_DIR as 'C:/My Projects/OTN/XML Schema/XML';
  2. 使用 DBMS_XMLSCHEMA 程序包在 XML DB 中注冊 XML 模式。 -- register the xml schema with xml dbbegindbms_xmlschema.registerSchema(schemaurl=>'http://localhost:8080/home/OTNXML/source/xsd/Player.xsd',schemadoc=>bfilename('OTNXML_DIR','Player.xsd'),local=>TRUE,gentypes=>TRUE,genbean=>FALSE,force=>FALSE,owner=>'OTNXML',csid=>nls_charset_id('AL32UTF8'));end;/

現在已經成功在數據庫中注冊了該模式,您可以查看在注冊過程中為您創建的對象。這些對象名稱和類型是在模式定義文件中包含的批注以及系統生成的名稱和類型的一個結果。例如,在第 2 步中,您指定了復雜類型的默認類型應當存儲在稱為“PLAYER_T”的 Oracle 類型中。下面您可以看到 Oracle 已經為您創建了該類型。 select object_name,object_type,statusfrom user_objectsorder by object_name,object_type;Results of the statement:OBJECT_NAME OBJECT_TYPE STATUS-------------------------------- ------------------- -------EXTRADATA177_L LOB VALIDNAMESPACES178_L LOB VALIDPLAYER TABLE VALIDPLAYER$xd TRIGGER VALIDPLAYER_T TYPE VALIDSYS_C004803 INDEX VALIDSYS_XDBPD6_L LOB VALID7 rows selected.第 4 步:加載 XML 實例文檔 為了演示加載 XML 實例文檔,我創建了一個符合在“Player.xsd”文件中所指定模式的小文件。與“Player.xsd”文件類似,該文件同樣是一個使用 Notepad 創建的簡單文本文件。“Player.xml”文件的內容如下: <?xml version="1.0"?><Playerxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://localhost:8080/home/OTNXML/source/xsd/Player.xsd"><PlayerName>Steven Gerrard</PlayerName><PlayerPosition>Midfield</PlayerPosition><PlayerNumber>8</PlayerNumber><PlayerClub>Localhost</PlayerClub></Player>該文件通過在文檔的根元素中包含以下文本被指定為 XML 實例文檔: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"此外,您可以看到在文檔根元素中還引用了剛才注冊的模式: xsi:noNamespaceSchemaLocation="http://localhost:8080/home/OTNXML/source/xsd/Player.xsd">我選擇在與模式文檔相同的目錄(在我的機器上為“C:/My Projects/OTN/XML Schema/XML”)中創建 XML 實例文檔以重用目錄對象。可用下列語句加載實例文檔: insert into playervalues(XMLType (bfilename('OTNXML_DIR','Player.xml'),nls_charset_id('AL32UTF8') ));注重:請務必在加載數據后執行 commit。 為了快速驗證數據是否已加載,您可以在 SQL*Plus 中輸入下列語句: select object_value from player;Results of the statement:OBJECT_VALUE-------------------------------------------------------------------------------<?xml version="1.0"?><Player xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost:8080/home/OTNXML/source/xsd/Player.xsd"><PlayerName>Steven Gerrard</PlayerName><PlayerPosition>Midfield</PlayerPosition><PlayerNumber>8</PlayerNumber><PlayerClub>Localhost</PlayerClub></Player>1 row selected.正如您所看到的那樣,該語句生成了數據的 XML 表示(即實例文檔)。 第 5 步:在 XML 數據上執行 DML 操作 您已經完成了所有的安裝工作,現在該運行 Visual Studio.NET,創建一個在數據庫中操作 XML 數據的應用程序了。在運行 Visual Studio.NET 之后,創建一個新的 Windows Forms 項目,然后在項目中添加一個對 Oracle Data Provider for .NET 的引用。應用程序包含一個簡單的表單,該表單將用來執行以下任務:
  • 連接數據庫
  • 檢索運動員
  • 插入運動員
  • 更新運動員
  • 刪除運動員

我將先提供完成這些任務的代碼。在提供代碼之后,我將實際演示該應用程序。為了執行這些任務,請創建類似下面的一個表單: 當然,執行這些操作的第一步是創建一個到數據庫的連接。我已經創建了一個表單級的字段,該字段代表連接。該字段被聲明為: // C#:private OracleConnection conn;'// VB.NET:Private conn As OracleConnection該字段在表單加載事件中進行初始化(如下): // C#:private void MainForm_Load(object sender, System.EventArgs e){// create new instance of Oracle Connection classconn = new OracleConnection();}'// VB.NET:Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load'// create new instance of Oracle Connection classconn = New OracleConnectionEnd SubConnect 按鈕背后的代碼將創建到數據庫的連接。這些代碼本質上相當于樣板。它以您先前創建的 OTNXML 用戶的身份創建一個到數據庫的連接。一旦成功連接,它就將禁用 Connect 按鈕,啟用其余按鈕(我創建這些按鈕時將它們禁用了),最后顯示一條消息,表明成功建立了一個連接。假如出現異常,該代碼將顯示一個基本消息框,指示出現了什么異常。 // C#:private void BTnConnect_Click(object sender, System.EventArgs e){Cursor.Current = Cursors.WaitCursor;// adjust the connection string as necessary for your environmentstring connstr = "User Id=OTNXML; PassWord=DEMO; Data Source=ORANET";// set the connection string value for the connection objectconn.ConnectionString = connstr;// attempt to open the connection// if failure display error message// if sUCcess display simple success messagetry {conn.Open(); }catch (Exception ex) {MessageBox.Show(this, ex.Message, application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Error); }finally {if (conn.State == ConnectionState.Open) {// disable the connect buttonbtnConnect.Enabled = false;// enable the remaining buttonsbtnGetPlayer.Enabled = true;btnInsertPlayer.Enabled = true;btnUpdatePlayer.Enabled = true;btnDeletePlayer.Enabled = true;MessageBox.Show(this, "Successfully connected to database.",Application.ProductName, MessageBoxButtons.OK,MessageBoxIcon.Information); } }Cursor.Current = Cursors.Default;}'// VB.NET:Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.ClickCursor.Current = Cursors.WaitCursor'// adjust the connection string as necessary for your environmentDim connstr As String = "User Id=OTNXML; Password=DEMO; Data Source=ORANET"'// set the connection string value for the connection objectconn.ConnectionString = connstr'// attempt to open the connection'// if failure display error message'// if success display simple success messageTryconn.Open()Catch ex As ExceptionMessageBox.Show(Me, ex.Message, Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Error)FinallyIf (conn.State = ConnectionState.Open) Then'// disable the connect buttonbtnConnect.Enabled = False'// enable the remaining buttonsbtnGetPlayer.Enabled = TruebtnInsertPlayer.Enabled = TruebtnUpdatePlayer.Enabled = TruebtnDeletePlayer.Enabled = TrueMessageBox.Show(Me, "Successfully connected to database.",Application.ProductName, MessageBoxButtons.OK,MessageBoxIcon.Information)End IfEnd TryCursor.Current = Cursors.DefaultEnd Sub
在與數據庫連接之后,您可以檢索一個運動員(比如說在上一步中加載的那個)。Get Player 按鈕背后的代碼將根據運動員的名稱來檢索運動員。檢索運動員的 SQL 語句將把運動員作為關系數據進行檢索,而不是檢索 XML 文檔本身。您將看到在代碼中使用 XML 文檔來插入運動員的一個示例。 // C#:private void btnGetPlayer_Click(object sender, System.EventArgs e){Cursor.Current = Cursors.WaitCursor;// build the sql statement to retrieve the xml// data as relational dataStringBuilder sbSQL = new StringBuilder();sbSQL.Append("select extractvalue(object_value, '/Player/PlayerPosition'), ");sbSQL.Append(" extractvalue(object_value, '/Player/PlayerNumber'), ");sbSQL.Append(" extractvalue(object_value, '/Player/PlayerClub') ");sbSQL.Append("from player ");sbSQL.Append("where existsNode(object_value, :1) = 1");// create an input parameter for the player nameOracleParameter p1 = new OracleParameter();p1.OracleDbType = OracleDbType.Varchar2;p1.Direction = ParameterDirection.Input;p1.Value = "/Player[PlayerName=/"" + txtPlayerName.Text + "/"]";OracleCommand cmd = new OracleCommand();cmd.CommandText = sbSQL.ToString();cmd.Connection = conn;cmd.Parameters.Add(p1);OracleDataReader dr = cmd.ExecuteReader();if (dr.Read()) {// assign the values to the text boxestxtPlayerPosition.Text = dr[0].ToString();txtPlayerNumber.Text = dr[1].ToString();txtPlayerClub.Text = dr[2].ToString();dr.Close(); }else {MessageBox.Show(this, "Player Not Found", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); }dr.Dispose();cmd.Dispose();Cursor.Current = Cursors.Default;}'// VB.NET:Private Sub btnGetPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPlayer.ClickCursor.Current = Cursors.WaitCursor'// build the sql statement to retrieve the xml'// data as relational dataDim sbSQL As StringBuilder = New StringBuildersbSQL.Append("select extractvalue(object_value, '/Player/PlayerPosition'), ")sbSQL.Append(" extractvalue(object_value, '/Player/PlayerNumber'), ")sbSQL.Append(" extractvalue(object_value, '/Player/PlayerClub') ")sbSQL.Append("from player ")sbSQL.Append("where existsNode(object_value, :1) = 1")'// create input parameter for the player nameDim p1 As OracleParameter = New OracleParameterp1.OracleDbType = OracleDbType.Varchar2p1.Direction = ParameterDirection.Inputp1.Value = "/Player[PlayerName=""" + txtPlayerName.Text + """]"Dim cmd As OracleCommand = New OracleCommandcmd.CommandText = sbSQL.ToString()cmd.Connection = conncmd.Parameters.Add(p1)Dim dr As OracleDataReader = cmd.ExecuteReader()If (dr.Read()) Then'// assign the values to the text boxestxtPlayerPosition.Text = dr(0).ToString()txtPlayerNumber.Text = dr(1).ToString()txtPlayerClub.Text = dr(2).ToString()dr.Close()ElseMessageBox.Show(Me, "Player Not Found", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)dr.Dispose()cmd.Dispose()Cursor.Current = Cursors.DefaultEnd IfEnd Sub
該應用程序提供了將運動員插入到數據庫中的功能,Ins Player 按鈕背后的代碼將插入一個符合您創建并加載到 XML DB 信息庫中的 XML 模式的新 XML 實例文檔。在創建了 XML 實例文檔之后,代碼將把保存該文檔的變量綁定到一個典型的 insert 語句中,執行 insert 操作,顯示一條成功或異常消息。 // C#:private void btnInsertPlayer_Click(object sender, System.EventArgs e){Cursor.Current = Cursors.WaitCursor;// create the xml documentStringBuilder xmlDocument = new StringBuilder();xmlDocument.Append("<?xml version=/"1.0/"?>");xmlDocument.Append("<Player");xmlDocument.Append("xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/"");xmlDocument.Append("xsi:noNamespaceSchemaLocation=/"http://localhost:8080/home/OTNXML/source/xsd/Player.xsd/">");xmlDocument.Append(" <PlayerName>" + txtPlayerName.Text + "</PlayerName>");xmlDocument.Append("<PlayerPosition>" + txtPlayerPosition.Text +"</PlayerPosition>");xmlDocument.Append("<PlayerNumber>" + txtPlayerNumber.Text +"</PlayerNumber>");xmlDocument.Append(" <PlayerClub>" + txtPlayerClub.Text + "</PlayerClub>");xmlDocument.Append("</Player>");// create command object and set propertiesOracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = "insert into player values (:1)";// create an input parameter to hold the xml documentOracleParameter p = new OracleParameter();p.Direction = ParameterDirection.Input;p.Value = xmlDocument.ToString();cmd.Parameters.Add(p);// execute the insert// display message if failuretry {cmd.ExecuteNonQuery();MessageBox.Show(this, "Player Inserted.", Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Information); }catch (Exception ex) {MessageBox.Show(this, ex.Message, Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Error); }p.Dispose();cmd.Dispose();Cursor.Current = Cursors.Default;}'// VB.NET:Private Sub btnInsertPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertPlayer.ClickCursor.Current = Cursors.WaitCursor'// create the xml documentDim xmlDocument As StringBuilder = New StringBuilderxmlDocument.Append("<?xml version=""1.0""?>")xmlDocument.Append("<Player")xmlDocument.Append("xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""")xmlDocument.Append("xsi:noNamespaceSchemaLocation=""http://localhost:8080/home/OTNXML/source/xsd/Player.xsd"">")xmlDocument.Append("<PlayerName>" + txtPlayerName.Text + "</PlayerName>")xmlDocument.Append("<PlayerPosition>" + txtPlayerPosition.Text +"</PlayerPosition>")xmlDocument.Append("<PlayerNumber>" + txtPlayerNumber.Text +"</PlayerNumber>")xmlDocument.Append("<PlayerClub>" + txtPlayerClub.Text + "</PlayerClub>")xmlDocument.Append("</Player>")'// create command object and set propertiesDim cmd As OracleCommand = New OracleCommandcmd.Connection = conncmd.CommandText = "insert into player values (:1)"'// create an input parameter to hold the xml documentDim p As OracleParameter = New OracleParameterp.Direction = ParameterDirection.Inputp.Value = xmlDocument.ToString()cmd.Parameters.Add(p)'// execute the insert'// display message if failureTrycmd.ExecuteNonQuery()MessageBox.Show(Me, "Player Inserted.", Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Information)Catch ex As ExceptionMessageBox.Show(Me, ex.Message, Application.ProductName,MessageBoxButtons.OK, MessageBoxIcon.Error)p.Dispose()cmd.Dispose()End TryCursor.Current = Cursors.DefaultEnd Sub
與插入運動員的代碼類似,Updt Player 按鈕背后的代碼將創建一個 XML 實例文檔,將其與 SQL 語句中的一個占位符變量綁定,并執行該語句來更新運動員。SQL 語句在 where 子句中使用運動員的名稱來確定更新哪個運動員。更新實際上是用一個新的包含更新數據的 XML 實例文檔替換現有的 XML 實例文檔。代碼完成后將顯示一條簡單的成功或異常消息。 // C#:private void btnUpdatePlayer_Click(object sender, System.EventArgs e){Cursor.Current = Cursors.WaitCursor;// create the xml document that contains the changesStringBuilder xmlDocument = new StringBuilder();xmlDocument.Append("<?xml version=/"1.0/"?>");xmlDocument.Append("<Player");xmlDocument.Append(" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/"");xmlDocument.Append(" xsi:noNamespaceSchemaLocation=/"http://localhost:8080/home/OTNXML/source/xsd/Player.xsd/">");xmlDocument.Append(" <PlayerName>" + txtPlayerName.Text + "</PlayerName>");xmlDocument.Append(" <PlayerPosition>" + txtPlayerPosition.Text + "</PlayerPosition>");xmlDocument.Append(" <PlayerNumber>" + txtPlayerNumber.Text + "</PlayerNumber>");xmlDocument.Append(" <PlayerClub>" + txtPlayerClub.Text + "</PlayerClub>");xmlDocument.Append("</Player>");StringBuilder sbSQL = new StringBuilder();sbSQL.Append("update player ");sbSQL.Append("set object_value = :1 ");sbSQL.Append("where existsNode(object_value, :2) = 1");// create an input parameter to hold the xml documentOracleParameter p1 = new OracleParameter();p1.OracleDbType = OracleDbType.Varchar2;p1.Direction = ParameterDirection.Input;p1.Value = xmlDocument.ToString(); // create an input parameter for the player nameOracleParameter p2 = new OracleParameter();p2.OracleDbType = OracleDbType.Varchar2;p2.Direction = ParameterDirection.Input;p2.Value = "/Player[PlayerName=/"" + txtPlayerName.Text + "/"]";// create command object and set propertiesOracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = sbSQL.ToString();cmd.Parameters.Add(p1);cmd.Parameters.Add(p2); // execute the update// display message if failuretry {cmd.ExecuteNonQuery();MessageBox.Show(this, "Player Updated.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); }catch (Exception ex) {MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); }p2.Dispose();p1.Dispose();cmd.Dispose();Cursor.Current = Cursors.Default;}'// VB.NET:Private Sub btnUpdatePlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdatePlayer.ClickCursor.Current = Cursors.WaitCursor'// create the xml document that contains the changesDim xmlDocument As StringBuilder = New StringBuilderxmlDocument.Append("<?xml version=""1.0""?>")xmlDocument.Append("<Player")xmlDocument.Append(" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""")xmlDocument.Append(" xsi:noNamespaceSchemaLocation=""http://localhost:8080/home/OTNXML/source/xsd/Player.xsd"">")xmlDocument.Append(" <PlayerName>" + txtPlayerName.Text + "</PlayerName>")xmlDocument.Append(" <PlayerPosition>" + txtPlayerPosition.Text + "</PlayerPosition>")xmlDocument.Append(" <PlayerNumber>" + txtPlayerNumber.Text + "</PlayerNumber>")xmlDocument.Append(" <PlayerClub>" + txtPlayerClub.Text + "</PlayerClub>")xmlDocument.Append("</Player>")Dim sbSQL As StringBuilder = New StringBuildersbSQL.Append("update player ")sbSQL.Append("set object_value = :1 ")sbSQL.Append("where existsNode(object_value, :2) = 1")'// create an input parameter to hold the xml documentDim p1 As OracleParameter = New OracleParameterp1.OracleDbType = OracleDbType.Varchar2p1.Direction = ParameterDirection.Inputp1.Value = xmlDocument.ToString()'// create an input parameter for the player nameDim p2 As OracleParameter = New OracleParameterp2.OracleDbType = OracleDbType.Varchar2p2.Direction = ParameterDirection.Inputp2.Value = "/Player[PlayerName=""" + txtPlayerName.Text + """]"'// create command object and set propertiesDim cmd As OracleCommand = New OracleCommandcmd.Connection = conncmd.CommandText = sbSQL.ToString()cmd.Parameters.Add(p1)cmd.Parameters.Add(p2)'// execute the update'// display message if failureTrycmd.ExecuteNonQuery()MessageBox.Show(Me, "Player Updated.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)Catch ex As ExceptionMessageBox.Show(Me, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)p1.Dispose()p2.Dispose()cmd.Dispose()End TryCursor.Current = Cursors.DefaultEnd Sub
該應用程序提供的最后的 DML 操作是刪除運動員的功能。與 retrieveupdate 代碼類似,這些代碼使用運動員的名稱作為確定對哪個運動員執行該操作的基礎。 // C#:private void btnDeletePlayer_Click(object sender, System.EventArgs e){Cursor.Current = Cursors.WaitCursor;// build the delete statementStringBuilder sbSQL = new StringBuilder();sbSQL.Append("delete ");sbSQL.Append("from player ");sbSQL.Append("where existsNode(object_value, :1) = 1");// create an input parameter for the player nameOracleParameter p1 = new OracleParameter();p1.OracleDbType = OracleDbType.Varchar2;p1.Direction = ParameterDirection.Input;p1.Value = "/Player[PlayerName=/"" + txtPlayerName.Text + "/"]";// create command object and set propertiesOracleCommand cmd = new OracleCommand();cmd.Connection = conn;cmd.CommandText = sbSQL.ToString();cmd.Parameters.Add(p1);// execute the deletetry {cmd.ExecuteNonQuery();MessageBox.Show(this, "Player Deleted.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); }catch (Exception ex) {MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); }cmd.Dispose();Cursor.Current = Cursors.WaitCursor;}'// VB.NET:Private Sub btnDeletePlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeletePlayer.ClickCursor.Current = Cursors.WaitCursor'// build the delete statementDim sbSQL As StringBuilder = New StringBuildersbSQL.Append("delete ")sbSQL.Append("from player ")sbSQL.Append("where existsNode(object_value, :1) = 1")'// create an input parameter for the player nameDim p1 As OracleParameter = New OracleParameterp1.OracleDbType = OracleDbType.Varchar2p1.Direction = ParameterDirection.Inputp1.Value = "/Player[PlayerName=""" + txtPlayerName.Text + """]"'// create command object and set propertiesDim cmd As OracleCommand = New OracleCommandcmd.Connection = conncmd.CommandText = sbSQL.ToString()cmd.Parameters.Add(p1)'// execute the deleteTrycmd.ExecuteNonQuery()MessageBox.Show(Me, "Player Deleted.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)Catch ex As ExceptionMessageBox.Show(Me, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)cmd.Dispose()End TryCursor.Current = Cursors.WaitCursorEnd Sub為了清除該表單,Reset 按鈕背后的代碼將簡單地將文本框的值設為空字符串。 // C#:private void btnReset_Click(object sender, System.EventArgs e){// "clear" the text box valuestxtPlayerName.Text = "";txtPlayerPosition.Text = "";txtPlayerNumber.Text = "";txtPlayerClub.Text = "";// assign focus to the player name text boxtxtPlayerName.Focus();}'// VB.NET:Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click'// "clear" the text box valuestxtPlayerName.Text = ""txtPlayerPosition.Text = ""txtPlayerNumber.Text = ""txtPlayerClub.Text = ""'// assign focus to the player name text boxtxtPlayerName.Focus()End Sub
正如您可能預料到的那樣,Close 按鈕背后的代碼僅執行表單關閉。 // C#:private void btnClose_Click(object sender, System.EventArgs e){// simply close the formthis.Close();}'// VB.NET:Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click'// simply close the formMe.Close()End Sub表單關閉事件中的代碼將檢查數據庫連接對象是否處于打開狀態;假如是,則將關閉并刪除該對象。 // C#:private void MainForm_Closing(object sender,System.ComponentModel.CancelEventArgs e){if (conn.State == ConnectionState.Open) {// close connection and dispose connection objectconn.Close();conn.Dispose();conn = null; }}'// VB.NET:Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.ClosingIf (conn.State = ConnectionState.Open) Then'// close connection and dispose connection objectconn.Close()conn.Dispose()conn = NothingEnd IfEnd Sub第 6 步:運行應用程序 現在您已經創建和加載了 XML 模式和 XML 實例文檔,并創建了應用程序,現在該看看它實際工作的情形了。當應用程序啟動時,表單的初始外觀將與下面類似: 在您單擊 Connect 按鈕之后,表單將顯示一個消息框,指示成功建立連接,并且應啟用了“DML 按鈕”: 要查看您在第 5 步中為 XML 實例文檔加載的運動員數據,請用運動員的名稱(Steven Gerrard,假如您使用與我相同數據的話)填充 Player Name 文本框,并單擊 Get Player 按鈕: 要創建新運動員,您可以單擊 Reset 按鈕,然后在文本框中輸入新信息,或者簡單地替換現有文本: 一旦您對各種運動員屬性的值感到滿足,即可單擊 Ins Player 按鈕。包含運動員信息的新 XML 實例文檔將被插入到數據庫中,并顯示一個消息框: 要更新運動員,您可以只要修改屬性(運動員名稱除外)即可。例如,我將運動員的號碼更改為 100: 單擊 Updt Player 按鈕,更新數據庫中的數據: 不幸地是,我似乎不符合 Localhost Football Club 的要求,因此我必須刪除我自己。為了刪除運動員,只需單擊 Del Player 按鈕即可: 要驗證運動員是否已被刪除,請單擊 Reset 按鈕,然后在 Player Name 文本框中輸入運動員名稱: 現在,單擊 Get Player 按鈕將顯示一個消息框,指示找不到請求的運動員: 當您完成測試時,單擊 Close 按鈕,終止應用程序。 總結 在本文中,我向您介紹了如何在 XML DB 信息庫中加載和注冊 XML 模式文檔,以及如何將符合模式的 XML 實例文檔加載到數據庫中。然后我創建了一個簡單的 Windows 表單應用程序,它在 XML 數據上執行 DML 操作。最后我演練了該應用程序的使用。這些信息將使您能開始從您基于 Oracle 的 .NET 應用程序在數據庫中使用 XML 數據。
Mark A. Williams 是《.NET 專家 Oracle 編程》(Apress,2004 年 11 月)的作者,目前在醫療診斷行業擔任 Production DBA。他自從 Oracle 數據庫的 7.0.1.16 版本起一直在使用 Oracle 產品,并且是版本 7、8、8i 和 9i 的 Oracle 認證數據庫治理員專家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲国产精品久久久 | 黄色a级片免费观看 | 精品久久久久久久 | 国产午夜精品理论片a级探花 | 黄色av网| 久久久久久亚洲国产精品 | 国产日产精品一区四区介绍 | 久久精品视频69 | 黄色片小说 | 久久精品79国产精品 | 黄视频在线网站 | 精品国产一区二区三区四 | 毛片大全免费 | 亚洲第一成人在线观看 | 欧美在线观看视频网站 | 99精品视频99| 亚洲成人免费影视 | 叶子楣成人爽a毛片免费啪啪 | 97zyz成人免费视频 | 国产精品久久久免费 | av国产免费 | 亚洲成人国产综合 | 中国免费一级毛片 | 亚洲一区二区三区四区精品 | 91 在线观看| 92看片淫黄大片欧美看国产片 | 免费a级黄色片 | 麻豆蜜桃在线观看 | 精品久久中文字幕 | 爱草在线| 曰韩一二三区 | 精品国产乱码久久久久久丨区2区 | 欧美在线 | 亚洲 | 欧美一级黄色片免费观看 | 狠狠操人人干 | 国产黄色网 | 久久精品一区二区三区四区五区 | 九九精品在线 | 91麻豆精品国产91久久久更新资源速度超快 | 黄色大片网站在线观看 | 精品国产一区二区三区四区阿崩 |