Condividi tramite


Eseguire un'operazione di inserimento nel database Oracle usando il modello di canale WCF

Questa sezione illustra come inserire un record in un database Oracle usando un canale. È necessario specificare sia un corpo del messaggio che un'azione di messaggio quando si invia un messaggio.

Messaggio di inserimento

Il codice XML seguente mostra il corpo di un messaggio per un'operazione di inserimento nella hr. Tabella EMPLOYEES. Il set di record è costituito da un singolo record dipendente. Per altre informazioni sullo schema di un messaggio di inserimento, vedere Schemi messaggio per operazioni di inserimento, aggiornamento, eliminazione e selezione di operazioni nelle tabelle e nelle viste di base. Questo è il contenuto del file Employee_Insert.xml usato nell'esempio.

<!-- New namespace: http://Microsoft.LobServices.OracleDB/2007/03/HR/Table/EMPLOYEES -->  
<Insert xmlns="http://Microsoft.LobServices.OracleDB/2007/03/HR/Table/EMPLOYEES">  
    <RECORDSET xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
        <EMPLOYEESRECORDINSERT>  
            <EMPLOYEE_ID>0</EMPLOYEE_ID>  
            <FIRST_NAME>Anton</FIRST_NAME>  
            <LAST_NAME>Kirilov</LAST_NAME>  
            <EMAIL></EMAIL>  
            <PHONE_NUMBER>555-0198</PHONE_NUMBER>  
            <HIRE_DATE>2007-03-01T00:00:00.0000000</HIRE_DATE>  
            <JOB_ID>FI_ACCOUNT</JOB_ID>  
            <SALARY>5000</SALARY>  
            <COMMISSION_PCT>0.15</COMMISSION_PCT>  
            <MANAGER_ID>108</MANAGER_ID>  
            <DEPARTMENT_ID>100</DEPARTMENT_ID>  
       </EMPLOYEESRECORDINSERT>  
    </RECORDSET>  
</Insert>  

Specifica dell'azione messaggio

È necessario specificare un'azione di messaggio quando si invia un messaggio SOAP all'adapter Oracle Database. È possibile specificare l'azione del messaggio quando si crea il messaggio come nell'esempio seguente.

Message messageIn2 = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.LobServices.OracleDB/2007/03/HR/Table/EMPLOYEES/Insert", readerIn2);  

L'azione messaggio in questo esempio, "/HR/Table/EMPLOYEES/Insert", specifica che un'operazione Di inserimento nella hr. La tabella EMPLOYEES deve essere eseguita

Invio del messaggio di inserimento

In questo esempio viene illustrato come eseguire un'operazione di inserimento su una tabella Oracle su un canale. Il codice usa l'operazione SQLEXECUTE esposta dall'adapter Oracle Database per restituire il valore successivo di oracle SEQUENCE. Questo valore viene quindi scritto nel campo EMPLOYEE_ID nel record Inserisci. Questo modello consente di inserire righe in database con un valore di chiave primaria generato automaticamente. Per altre informazioni sulla chiamata dell'operazione SQLEXECUTE su un canale, vedere Eseguire un'operazione SQLEXECUTE tramite il modello di canale WCF.

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Xml;  
using System.IO;  
  
using System.ServiceModel;  
using System.ServiceModel.Channels;  
  
using Microsoft.ServiceModel.Adapters;  
using Microsoft.Adapters.OracleDB;  
  
namespace OracleDMLChannel  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Create Endpoint  
            EndpointAddress address = new EndpointAddress("oracledb://ADAPTER");  
  
            // Create Binding  
            OracleDBBinding binding = new OracleDBBinding();  
  
            // Create Channel Factory  
            ChannelFactory<IRequestChannel> factory = new ChannelFactory<IRequestChannel>(binding, address);  
            factory.Credentials.UserName.UserName = "HR";  
            factory.Credentials.UserName.Password = "TIGER";  
            factory.Open();  
  
            // Create Request Channel  
            IRequestChannel channel = factory.CreateChannel();  
            channel.Open();  
  
            // Send Request  
            System.Xml.XmlReader readerIn = System.Xml.XmlReader.Create("SQLExecute.xml");  
  
            Message messageIn = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.LobServices.OracleDB/2007/03/SQLEXECUTE", readerIn);  
            Message messageOut = channel.Request(messageIn);  
  
            // Get Response XML  
            XmlReader readerOut = messageOut.GetReaderAtBodyContents();  
  
            // Get Employee ID  
            string id = null;  
            XmlDocument doc = new XmlDocument();  
            doc.Load(readerOut);  
            XmlNodeList list = doc.GetElementsByTagName("ColumnValue");  
            if (list.Count > 0) id = list[0].InnerXml;  
  
            // Compose Insert XML  
            XmlDocument insertDoc = new XmlDocument();  
            insertDoc.Load("Employee_Insert.xml");  
  
            // Change Employee ID  
            XmlNodeList empidList = insertDoc.GetElementsByTagName("EMPLOYEE_ID");  
            XmlNode empidNode = empidList[0];  
            empidNode.InnerXml = id;  
  
            // Change email  
            XmlNodeList emailList = insertDoc.GetElementsByTagName("EMAIL");  
            XmlNode emailNode = emailList[0];  
            emailNode.InnerXml = "scotty" + id + "@microsoft.com";  
  
            // Change date  
            XmlNodeList dateList = insertDoc.GetElementsByTagName("HIRE_DATE");  
            XmlNode dateNode = dateList[0];  
            dateNode.InnerXml = "2007-03-01T00:00:00.0000000";  
  
            StringReader strReader = new StringReader(insertDoc.InnerXml);  
            XmlReader readerIn2 = XmlReader.Create(strReader);  
  
            // Send XML  
            Message messageIn2 = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.LobServices.OracleDB/2007/03/HR/Table/EMPLOYEES/Insert ", readerIn2);  
            Message messageOut2 = channel.Request(messageIn2);  
  
            // Close the messages  
            messageOut.Close();  
            messageOut2.Close();  
  
            channel.Close();  
        }  
    }  
}  

Vedere anche

Sviluppare applicazioni di database Oracle usando il modello di canale WCF
Creare un canale con Oracle Database
Eseguire un'operazione SQLEXECUTE usando il modello di canale WCF
Richiamare una funzione nel database Oracle usando il modello di canale WCF