SQLXMLBulkLoad中的错误

时间:2014-07-17 18:47:06

标签: xml c#-4.0 sql-server-2012 bulk-load

我错过了什么,或者这是一个SQL Server错误!?当我使用批量加载时,对于来自其他表的外键的字段,我得到0。父表没有任何带有键0 !!

的记录

以下是数据库中的表格:

CREATE TABLE xmlAction(
    ActionID int IDENTITY(1,1) PRIMARY KEY,
    ActionGUID nvarchar(50) NOT NULL);
CREATE TABLE xmlDomain(
    DomainID  int IDENTITY(1,1) PRIMARY KEY,
    ActionID int NOT NULL FOREIGN KEY REFERENCES xmlAction(ActionID),
    DomainName nvarchar(256) NOT NULL);
CREATE TABLE xmlSid(
    SidID int IDENTITY(1,1) PRIMARY KEY,
    SidString nvarchar(256) NOT NULL  ,
    SidType nvarchar(50) NULL,
    DomainID int NULL FOREIGN KEY REFERENCES xmlDomain(DomainID));
    CREATE UNIQUE INDEX UniqueSidString ON xmlSid(SidString);
CREATE TABLE xmlGroup(
    GroupSidID int PRIMARY KEY FOREIGN KEY REFERENCES xmlSid(SidID),
    GroupName nvarchar(256) NULL,
    GroupComments nvarchar(512) NULL);
CREATE TABLE xmlSidMembership(
    GroupSidID int NOT NULL FOREIGN KEY REFERENCES xmlGroup(GroupSidID),
    SidString nvarchar(256) NOT NULL);

这是我的架构,将其另存为Test.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">

  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="ActionDomain"
      parent="xmlAction"
      parent-key="ActionID"
      child="xmlDomain"
      child-key="ActionID" />
    </xsd:appinfo>
  </xsd:annotation>

  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="DomainComputer"
      parent="xmlDomain"
      parent-key="DomainID"
      child="xmlComputer"
      child-key="DomainID" />
    </xsd:appinfo>
  </xsd:annotation>

  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="DomainSid"
      parent="xmlDomain"
      parent-key="DomainID"
      child="xmlSid"
      child-key="DomainID" />
    </xsd:appinfo>
  </xsd:annotation>


  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="SidGroup"
      parent="xmlSid"
      parent-key="SidID"
      child="xmlGroup"
      child-key="GroupSidID" />
    </xsd:appinfo>
  </xsd:annotation>

  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="GroupMembership"
      parent="xmlGroup"
      parent-key="GroupSidID"
      child="xmlSidMembership"
      child-key="GroupSidID" />
    </xsd:appinfo>
  </xsd:annotation>



  <xsd:element name="Action" sql:relation="xmlAction" >
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="ActionGUID"  type="xsd:string" />

        <xsd:element name="Domain"
        sql:relation="xmlDomain"
        sql:relationship="ActionDomain">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="DomainName" type="xsd:string" />

              <xsd:element name="Sid" 
              sql:relation="xmlSid"
              sql:relationship="DomainSid">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="SidString"  type="xsd:string" />
                    <xsd:element name="SidType" type="xsd:string" />

                    <xsd:element name="Group"
                    sql:relation="xmlGroup"
                    sql:relationship="SidGroup">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="GroupName"  type="xsd:string" />
                          <xsd:element name="GroupComments"  type="xsd:string" />

                          <xsd:element name="SidMembership"
                          sql:relation="xmlSidMembership"
                          sql:relationship="GroupMembership">
                            <xsd:complexType>
                              <xsd:sequence>
                                <xsd:element name="SidString"  type="xsd:string" />
                              </xsd:sequence>
                            </xsd:complexType>
                          </xsd:element>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

这是示例xml,将其另存为Test.xml:

<ROOT>
  <Action>
    <ActionGUID>MAction1</ActionGUID>
    <Domain>
      <DomainName>MDomain11</DomainName>
      <Sid>
        <SidString>SidString111</SidString>
        <SidType>Group</SidType>
        <SidName>Group111</SidName>
        <Group>
          <GroupName>Group1111</GroupName>
          <GroupComments>This group belongs to actin1 domain 1 sid 1</GroupComments>
          <SidMembership>
            <SidString>SidString1121</SidString>
          </SidMembership>
          <SidMembership>
            <SidString>SidString1121</SidString>
          </SidMembership>
          <SidMembership>
            <SidString>SidString1131</SidString>
          </SidMembership>
        </Group>
      </Sid>

以下是运行它的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace LoadXML
{
    class Program
    {
        private const string ServerName = "Server";
        private const string DBName = "DB;
        private const string UserName = "Username";
        private const string Password = "Password";
        private const string sPath = @"C:\Projects\LoadingXML\XMLFiles\";

        static void Main(string[] args)
        {
            BulkLoadXML("Test");
        }


        static string CreatePath(string FileName, string extension)
        {
            return sPath + FileName + extension;
        }

        static void BulkLoadXML(string FileName)
        {
            try
            {
                SQLXMLBULKLOADLib.SQLXMLBulkLoad4 objBL = new SQLXMLBULKLOADLib.SQLXMLBulkLoad4();
                objBL.ConnectionString = @"Provider=sqloledb; server=" + ServerName + ";database=" + DBName + ";User ID=" + UserName + ";Password=" + Password + ";Connection Timeout=60";
                objBL.KeepIdentity = false;
                objBL.Execute(CreatePath(FileName, ".xsd"), CreatePath(FileName, ".xml"));
                MessageBox.Show("Uploaded" + " " + FileName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString( ));
            }
        }
        }
    }
}

为什么表GroupSidID中的xmlSidMembership为0?父表xmlGroup没有此密钥! 谢谢!

1 个答案:

答案 0 :(得分:0)

您缺少ROOT节点元素描述

<xsd:element name="ROOT" sql:is-constant="1">
  <xsd:complexType>
    <xsd:sequence>

在此行之前

<xsd:element name="Action" sql:relation="xmlAction" >

并且不要忘记关闭XSD底部的标签。