Udostępnij przez


Wykonywanie element w formacie DiffGram przy użyciu SQLXML kontrolowany klasy

W tym przykładzie przedstawiono sposób wykonać pliku w formacie DiffGram w Microsoft Środowisko .NET framework do zastosowania aktualizacji danych SQL Server między tabelami przy użyciu klasy Managed SQLXML)Microsoft.Data.SqlXml).

W tym przykładzie w formacie DiffGram aktualizuje informacje o klientach (NazwaFirmy i nazwa kontaktu) dla odbiorcy ALFKI.

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql" sql:mapping-schema="DiffGramSchema.xml">
  <diffgr:diffgram 
           xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" 
           xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DataInstance>
      <Customer diffgr:id="Customer1" 
                msdata:rowOrder="0" diffgr:hasChanges="modified" 
                CustomerID="ALFKI">
          <CompanyName>Bottom Dollar Markets</CompanyName>
          <ContactName>Antonio Moreno</ContactName>
      </Customer>
    </DataInstance>
    <diffgr:before>
     <Customer diffgr:id="Customer1" 
               msdata:rowOrder="0" 
               CustomerID="ALFKI">
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria Anders</ContactName>
      </Customer>
    </diffgr:before>
  </diffgr:diffgram>
</ROOT>

The <before> blok includes a <Customer> element (diffgr:id="Customer1").The <DataInstance> blok includes the corresponding <Customer> element with same id.The <customer> element in the <NewDataSet> also specifies diffgr:hasChanges="modified".Oznacza to, że operacja aktualizacji, a rekord klienta w tabela Cust zostanie odpowiednio zaktualizowany.Należy zauważyć, że jeśli diffgr:hasChanges nie jest określony, logiki przetwarzania w formacie DiffGram ignoruje ten element, a aktualizacje nie są wykonywane.

Poniżej przedstawiono kod C# samouczka aplikacją, która pokazuje, jak wykonać powyższe w formacie DiffGram i zaktualizować utworzy również w obu tabelach (Cust zakupu) za pomocą klasy Managed SQLXML tempdb bazy danych.

using System;
using System.Data;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
   static string ConnString = "Provider=SQLOLEDB;Server=MyServer;database=tempdb;Integrated Security=SSPI;";
   public static int testParams()
   {
      SqlXmlAdapter ad;
      // Need a memory stream to hold diff gram temporarily
      MemoryStream ms = new MemoryStream();
      SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
      cmd.RootTag = "ROOT";
      cmd.CommandStream = new FileStream("MyDiffgram.xml", FileMode.Open, FileAccess.Read);
      cmd.CommandType = SqlXmlCommandType.DiffGram;
      cmd.SchemaPath = "DiffGramSchema.xml";
      // Load data set
      DataSet ds = new DataSet();
      ad = new SqlXmlAdapter(cmd);
      ad.Fill(ds);
      ad.Update(ds);
      return 0;
   }
   public static int Main(String[] args)
   {
      testParams();
      return 0;
   }
}

Aby przetestować aplikację

  1. Upewnij się, że system .NET Framework jest zainstalowana na komputerze.

  2. Zapisać następującego schematu XSD (DiffGramSchema.xml) w folderze:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
     <xsd:annotation>
      <xsd:documentation>
        Diffgram Customers/Orders Schema.
      </xsd:documentation>
      <xsd:appinfo>
           <sql:relationship name="CustomersOrders" 
                      parent="Cust"
                      parent-key="CustomerID"
                      child-key="CustomerID"
                      child="Ord"/>
      </xsd:appinfo>
     </xsd:annotation>
     <xsd:element name="Customer" sql:relation="Cust">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="CompanyName"    type="xsd:string"/>
          <xsd:element name="ContactName"    type="xsd:string"/>
           <xsd:element name="Order" sql:relation="Ord" sql:relationship="CustomersOrders">
            <xsd:complexType>
              <xsd:attribute name="OrderID" type="xsd:int" sql:field="OrderID"/>
              <xsd:attribute name="CustomerID" type="xsd:string"/>
            </xsd:complexType>
          </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="CustomerID" type="xsd:string" sql:field="CustomerID"/>
      </xsd:complexType>
     </xsd:element>
    </xsd:schema>
    
  3. Tworzenie tych tabel w tempdb bazy danych.

    CREATE TABLE Cust(
            CustomerID  nchar(5) Primary Key,
            CompanyName nvarchar(40) NOT NULL ,
            ContactName nvarchar(60) NULL)
    GO
    
    CREATE TABLE Ord(
       OrderID    int Primary Key,
       CustomerID nchar(5) Foreign Key REFERENCES Cust(CustomerID))
    GO
    
  4. Dodaj dane przykładowe to:

    INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
         (N'ALFKI', N'Alfreds Futterkiste', N'Maria Anders')
    INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
         (N'ANATR', N'Ana Trujillo Emparedados y helados', N'Ana Trujillo')
    INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
         (N'ANTON', N'Antonio Moreno Taquería', N'Antonio Moreno')
    
    INSERT INTO Ord(OrderID, CustomerID) VALUES(1, N'ALFKI')
    INSERT INTO Ord(OrderID, CustomerID) VALUES(2, N'ANATR')
    INSERT INTO Ord(OrderID, CustomerID) VALUES(3, N'ANTON')
    
  5. Skopiuj formacie DiffGram powyżej i wkleić go do pliku tekstowego.Zapisz plik jako MyDiffGram.xml w tym samym folderze, w kroku 1.

  6. Zapisz C# kod (DiffgramSample.cs), który znajduje się powyżej w tym samym folderze, w którym DiffGramSchema.xml i MyDiffGram.xml były przechowywane w poprzednich krokach.

    Uwaga

    Trzeba będzie zaktualizować nazwę SQL Server wystąpienie w ciąg połączenia „MyServer"na nazwę rzeczywistego wystąpienia zainstalowanego programu SQL Server.

    Jeśli pliki są przechowywane w innym folderze, należy edytować kod i określ ścieżka katalogu właściwe dla mapowania schematu.

  7. Skompiluj kod.Aby skompilować kod w wiersz polecenia, należy użyć:

    csc /reference:Microsoft.Data.SqlXML.dll DiffgramSample.cs
    

    Spowoduje to utworzenie pliku wykonywalnego (DiffgramSample.exe).

  8. W wiersz polecenia należy wykonać DiffgramSample.exe.