共用方式為


使用 WCF 服務模型在 Oracle 資料庫中使用 REF CURSORS 執行作業

REF CURSOR 是 Oracle PL/SQL 資料類型,代表 Oracle 資料庫中結果集的指標。 Microsoft BizTalk Adapter for Oracle Database 支援程式、函式和封裝中的 REF CURSOR 參數。 REF CURSOR 參數可以是強型別或弱型別,視它們在程式或函式中宣告的方式而定。 如需 ORACLE 資料庫配接器如何代表 REF CURSOR 參數的詳細說明,請參閱 REF CURSORS 的訊息架構。下表摘要說明 WCF 服務模型中如何呈現 REF CURSOR 參數。

參數方向 強型別 REF CURSOR 弱型別 REF CURSOR
IN string [PARAM_NAME]

包含 PL/SQL 區塊的字串。 PL/SQL 區塊必須藉由執行 「OPEN FOR SELECT」 語句或叫用函式或程式,傳回已開啟的 REF CURSOR。 問號 (?) 表示傳回參數的 REF CURSOR 位置。 例如,「BEGIN OPEN ? FOR SELECT * FROM MY_TABLE;END「, 或 」BEGIN MY_PROC (PARM1, ?, PARM2) ;END;「。
與強型別相同
OUT out [PROC_NS].[PARAM_NAME]RECORD[] [PARAM_NAME]

強型別的記錄集。
out [GENERIC_NS].GenRecordRow[] [PARAM_NAME]

弱型別的泛型記錄集。
IN OUT IN OUT REF CURSOR 參數會分割成 IN 和 OUT 參數。 IN 參數會附加方法簽章中的 「_IN」,以區別它與 OUT 參數。 OUT 參數是以強型別的記錄集表示。

string [PARAM_NAME]_IN

out [PROC_NS].[PARAM_NAME]RECORD[] [PARAM_NAME]
IN OUT REF CURSOR 參數會分割成 IN 和 OUT 參數。 IN 參數會附加 「_IN」,以區別它與 OUT 參數。 OUT 參數是以弱型別的記錄集表示。

string [PARAM_NAME]_IN

out [GENERIC_NS].GenRecordRow[] [PARAM_NAME]

[PARAM_NAME] = Oracle 資料庫上函式或程式定義中的參數名稱;例如,MYREFCURSOR。

[PROC_NS] = 產生以包含封裝、程式或函式參數的唯一命名空間;例如,「microsoft.lobservices.oracledb._2007._03.SCOTT。Package.ACCOUNT_PKG。GET_ACTIVITY」。

[GENERIC_NS] = 定義泛型記錄集的命名空間「microsoft.lobservices.oracledb._2007._03」。

關於本主題中使用的範例

本主題中的範例使用 /SCOTT/Package/ACCOUNT_PKG Oracle PACKAGE。 下列程式用於ACCOUNT_PKG:

PROCEDURE get_activity(inrecs IN SYS_REFCURSOR, status OUT NUMBER, inoutrecs IN OUT activity_ref_type, outrecs OUT SYS_REFCURSOR);  

產生此套件的腳本會隨附 SDK 範例。 如需 SDK 範例的詳細資訊,請參閱 SDK 中的範例

WCF 服務模型中的 REF CURSOR 參數

下列範例顯示針對 /SCOTT/Package/ACCOUNT_PKG/GET_ACTIVITY 程式產生的類別和 WCF 用戶端。 此程式具有弱型別 IN 和 OUT REF CURSOR 參數,以及強型別 IN OUT REF CURSOR 參數。

以下是在 WCF 用戶端中產生以叫用GET_ACTIVITY的方法簽章。

public System.Nullable<decimal> GET_ACTIVITY(string INRECS, string INOUTRECS_IN, out microsoft.lobservices.oracledb._2007._03.SCOTT.Package.ACCOUNT_PKG.GET_ACTIVITY.INOUTRECSRECORD[] INOUTRECS, out microsoft.lobservices.oracledb._2007._03.GenRecordRow[] OUTRECS);  

GET_ACTIVITY 方法中,IN OUT 參數 INOUTRECS 分成兩個參數:

  • INOUTRECS_IN是代表 IN REF CURSOR 參數的字串。

  • INOUTRECS 是一個強型別的記錄集,代表 OUT REF CURSOR 參數。

    弱型別的 OUT 參數 OUTRECS 會以一般記錄集表示。 弱型別 IN 參數 INRECS 會以字串表示。

Strongly-Typed OUT REF CURSOR 參數

強型別的 OUT (或 IN OUT) REF CURSOR 參數會根據使用它們的程式或函式名稱,在唯一命名空間中產生。 針對 /SCOTT/Package/ACCOUNT_PKG/GET_ACTIVITY 程式,此命名空間為 microsoft.lobservices.oracledb._2007._03.SCOTT.Package.ACCOUNT_PKG.GET_ACTIVITY 。 類別名稱的形成方式是附加參數的名稱與 「RECORD」,而 類別是由代表 Oracle 欄位的屬性所組成。 以下顯示 類別的一部分,代表為 INOUTRECS REF CURSOR 參數產生的強型別記錄。

namespace microsoft.lobservices.oracledb._2007._03.SCOTT.Package.ACCOUNT_PKG.GET_ACTIVITY {  
    using System.Runtime.Serialization;  
  
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
    [System.Runtime.Serialization.DataContractAttribute()]  
    public partial class INOUTRECSRECORD : object, System.Runtime.Serialization.IExtensibleDataObject {  
  
        ...  
  
        private System.Nullable<decimal> TIDField;  
  
        ...  
  
        [System.Runtime.Serialization.DataMemberAttribute()]  
        public System.Nullable<decimal> TID {  
            get {  
                return this.TIDField;  
            }  
            set {  
                this.TIDField = value;  
            }  
        }  
  
        ...  
  
    }  
}  

Weakly-Typed OUT REF CURSOR 參數

弱型別的 OUT (或 IN OUT) REF CURSOR 參數是由泛型記錄類別表示。 不論函式或程式為何,一律會在相同的命名空間中產生泛型記錄集,且具有相同類別名稱。 下列程式碼顯示泛型記錄類別 microsoft.lobservices.oracledb._2007._03.GenRecordRow,代表 OUTRECS OUT SYS_REFCURSOR 參數的記錄, (弱型別) 。

namespace microsoft.lobservices.oracledb._2007._03 {  
    using System.Runtime.Serialization;  
  
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
    [System.Runtime.Serialization.DataContractAttribute()]  
    public partial class GenRecordRow : object, System.Runtime.Serialization.IExtensibleDataObject {  
  
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;  
  
        private microsoft.lobservices.oracledb._2007._03.GenRecordColumn[] GenRecordColumnField;  
  
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData {  
            get {  
                return this.extensionDataField;  
            }  
            set {  
                this.extensionDataField = value;  
            }  
        }  
  
        [System.Runtime.Serialization.DataMemberAttribute()]  
        public microsoft.lobservices.oracledb._2007._03.GenRecordColumn[] GenRecordColumn {  
            get {  
                return this.GenRecordColumnField;  
            }  
            set {  
                this.GenRecordColumnField = value;  
            }  
        }  
    }  
  
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
    [System.Runtime.Serialization.DataContractAttribute()]  
    public partial class GenRecordColumn : object, System.Runtime.Serialization.IExtensibleDataObject {  
  
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;  
  
        private string ColumnNameField;  
  
        private string ColumnValueField;  
  
        private string ColumnTypeField;  
  
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData {  
            get {  
                return this.extensionDataField;  
            }  
            set {  
                this.extensionDataField = value;  
            }  
        }  
  
        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]  
        public string ColumnName {  
            get {  
                return this.ColumnNameField;  
            }  
            set {  
                this.ColumnNameField = value;  
            }  
        }  
  
        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]  
        public string ColumnValue {  
            get {  
                return this.ColumnValueField;  
            }  
            set {  
                this.ColumnValueField = value;  
            }  
        }  
  
        [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false, Order=2)]  
        public string ColumnType {  
            get {  
                return this.ColumnTypeField;  
            }  
            set {  
                this.ColumnTypeField = value;  
            }  
        }  
    }  
}  

搭配 WCF 用戶端使用 REF CURSOR 參數

若要使用 WCF 用戶端使用 REF CURSOR 參數叫用程式或函式,請執行下列動作:

  1. 針對包含 PL/SQL 區塊的每個 IN 或 IN OUT REF CURSOR 參數傳遞字串,以開啟 REF CURSOR。 此區塊可以執行 OPEN FOR SELECT 語句,或叫用函式或程式,以傳回 OUT 參數中開啟的 REF CURSOR。

  2. 當程式或函式傳回時,請針對任何 OUT 或 IN OUT REF CURSOR 參數傳回的記錄集中的資料操作。 記錄集將是弱型別 REF CURSOR 參數的一般記錄集,或是強型別 REF CURSOR 參數的強型別記錄集。

    如需如何使用 WCF 服務模型叫用程式和函式的詳細資訊,請參閱 使用 WCF 服務模型叫用 Oracle 資料庫中的函式和程式

    下列範例會呼叫 GET_ACTIVITY 程式。 它示範兩種指定 IN REF CURSOR 參數的方式:

  • 針對 IN REF CURSOR 參數,會指定 OPEN FOR SELECT 語句,以傳回 ACCOUNT 100001的活動。

  • 針對 IN OUT REF CURSOR 參數,會叫用 /SCOTT/Package/ACCOUNT_PKG/GET_ALL_ACTIVITY程式。 此程式會開啟 REF CURSOR,其中包含 ACCOUNTACTIVITY 資料表中的所有活動,並將它傳回為 OUT 參數。

    此範例也會示範如何從針對強型別和弱型別 REF CURSOR 參數傳回的記錄集讀取資料。

using System;  
using System.Collections.Generic;  
using System.Text;  
  
// Add WCF, WCF LOB Adapter SDK, and Oracle Database adapter namepaces  
using System.ServiceModel;  
using Microsoft.ServiceModel.Channels;  
using Microsoft.Adapters.OracleDB;  
  
// Include this namespace for WCF LOB Adapter SDK and Oracle Database adapter exceptions  
using Microsoft.ServiceModel.Channels.Common;  
  
// namespaces for strongly-typed and weakly typed REF CURSOR records  
using GET_ACTIVITYns = microsoft.lobservices.oracledb._2007._03.SCOTT.Package.ACCOUNT_PKG.GET_ACTIVITY;  
using GENERICns = microsoft.lobservices.oracledb._2007._03;  
  
// In this sample, INRECS is opened by using an OPEN FOR statement, and  
// INOUTRECS_IN is opened by calling the GET_ALL_ACTIVITY procedure on Oracle.  
  
namespace OracleRefCursorsSM  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Create the client  
            SCOTTPackageACCOUNT_PKGClient accountPkgClient =   
                new SCOTTPackageACCOUNT_PKGClient("OracleDBBinding_SCOTT.Package.ACCOUNT_PKG");  
            // Set credentials  
            accountPkgClient.ClientCredentials.UserName.UserName = "SCOTT";  
            accountPkgClient.ClientCredentials.UserName.Password = "TIGER";  
  
            try  
            {  
  
                GET_ACTIVITYns.INOUTRECSRECORD[] strongCursor;  
                GENERICns.GenRecordRow[] weakCursor;  
  
                Console.WriteLine("Opening client");  
                // Open the client  
                accountPkgClient.Open();  
  
                Console.WriteLine("Invoking ACCOUNT_PKG.GET_ACTIVITY");  
                // Get  ACCOUNTACTIVITY records  
                // The IN REF CURSOR is set to all activity for account 100001  
                // The input part of the IN OUT ref cursor calls GET_ALL_ACTIVITY  
                // The weakly-typed OUT REF CURSOR parameter returns a list of activity for account 100001  
                // The strongly-typed IN OUT REF CURSOR parameter returns a list of all activity  
                string inRecsString = "BEGIN OPEN ? FOR SELECT * FROM ACCOUNTACTIVITY WHERE ACCOUNT=100001; END;";  
                string inoutRecsString = "BEGIN ACCOUNT_PKG.GET_ALL_ACTIVITY(?); END;";  
  
                accountPkgClient.GET_ACTIVITY(  
                                inRecsString,  
                                inoutRecsString,  
                                out strongCursor,  
                                out weakCursor);  
  
                // Display strong ref cursor (all activity)  
                Console.WriteLine("\nList of all activity returned (strong ref cursor)");  
                Console.WriteLine("Tx Id\tAccount\tAmount\tDate\t\t\tDescription");  
                for (int i = 0; i < strongCursor.Length; i++)  
                {  
                    Console.WriteLine("{0}\t{1}\t{2:C}\t{3}\t{4}",strongCursor[i].TID,  
                        strongCursor[i].ACCOUNT,   
                        strongCursor[i].AMOUNT,   
                        strongCursor[1].TRANSDATE,  
                        strongCursor[i].DESCRIPTION);  
                }  
  
                // Display weak ref cursor (account 100001)  
                Console.WriteLine("\nList of activity for account 100001 returned (weak ref cursor)");  
                Console.WriteLine("Tx Id\tAmount\tDate\t\t\tDescription");  
                for (int i = 0; i < weakCursor.Length; i++)  
                {  
                    Console.WriteLine("{0}\t{1:C}\t{2}\t{3}", weakCursor[i].GenRecordColumn[0].ColumnValue,  
                        weakCursor[i].GenRecordColumn[2].ColumnValue,  
                        weakCursor[i].GenRecordColumn[4].ColumnValue,  
                        weakCursor[i].GenRecordColumn[3].ColumnValue);  
                }  
  
                Console.WriteLine("\nHit <RETURN> to finish");  
                Console.ReadLine();  
            }  
            catch (TargetSystemException tex)  
            {  
                Console.WriteLine("Exception occurred on the Oracle Database");  
                Console.WriteLine(tex.InnerException.Message);  
            }  
            catch (ConnectionException cex)  
            {  
                Console.WriteLine("Exception occurred connecting to the Oracle Database");  
                Console.WriteLine(cex.InnerException.Message);  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine("Exception is: " + ex.Message);  
                if (ex.InnerException != null)  
                {  
                    Console.WriteLine("Inner Exception is: " + ex.InnerException.Message);  
                }  
                throw ex;  
            }  
            finally  
            {  
                // Close the client  
                accountPkgClient.Close();  
            }  
        }  
    }  
}  

另請參閱

使用 WCF 服務模型開發 Oracle 資料庫應用程式