使用 SQLXML 托管类执行 DiffGram

本示例演示如何在 Microsoft .NET Framework 环境中执行 DiffGram 以使用 SQLXML 托管类 (Microsoft.Data.SqlXml) 将数据更新应用到 SQL Server 表。

在本例中,DiffGram 更新客户 ALFKI 的客户信息(CompanyName 和 ContactName)。

<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>

<before> 块包括 <Customer> 元素 (diffgr:id="Customer1")。<DataInstance> 块包括对应的 <Customer> 元素,并且具有相同的 id<NewDataSet> 中的 <customer> 元素还指定了 diffgr:hasChanges="modified"。这指示一个更新操作,而且 Cust 表中的客户记录也会相应地更新。请注意,如果未指定 diffgr:hasChanges 属性,则 DiffGram 处理逻辑将忽略此元素并且不执行任何更新。

以下是 C# 教程应用程序的代码,该应用程序演示如何使用 SQLXML 托管类执行上面的 DiffGram 和更新您将要在 tempdb 数据库中创建的两个表(Cust、Ord)。

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;
   }
}

测试应用程序

  1. 确保已在计算机上安装了 .NET Framework。

  2. 将以下 XSD 架构 (DiffGramSchema.xml) 保存在某个文件夹中:

    <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. tempdb 数据库中创建这些表。

    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. 添加该示例数据:

    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. 复制上面的 DiffGram,并将它粘贴到文本文件中。在与步骤 1 中所使用文件夹相同的文件夹中将文件另存为 MyDiffGram.xml。

  6. 将上面提供的 C# 代码 (DiffgramSample.cs) 保存到先前步骤中用于保存 DiffGramSchema.xml 和 MyDiffGram.xml 的文件夹中。

    注意注意

    您需要将连接字符串中 SQL Server 实例的名称从“MyServer”更新为所安装 SQL Server 实例的实际名称。

    如果将该文件存储在不同文件夹中,则必须编辑代码,为映射架构指定相应的目录路径。

  7. 编译代码。若要在命令提示符下编译代码,请使用:

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

    这将创建一个可执行文件 (DiffgramSample.exe)。

  8. 在命令提示符下,执行 DiffgramSample.exe。

请参阅

参考