Friday, March 9, 2012

Invalid cast when trying to use SQLXMLBulkload

I am trying to move data between two servers through my application by outputting the data as XML and then importing it later. I am using .WriteXML and .WriteXMLSchema in my .NET application to create the files, but I get "invalid cast Exception" when trying to BulkLoad them in using SQLXMLBulkLoad4. I even adjust the XSD file to add the sqlBig Smileatatype in there for the DateTime fields. Is there another field I am breaking on. I looked at the docs and I thinbk my GUIDs are OK since they do not have the {} on them.

Example file: XSD file

<?xml version="1.0" standalone="yes"?>
<xsTongue Tiedchema id="STG_Additional_Labs" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urnTongue Tiedchemas-microsoft-com:xml-msdata">
<xs:element name="STG_Additional_Labs" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xsTongue Tiedequence>
<xs:element name="FacilityID" type="xs:int" minOccurs="0" />
<xs:element name="Comment" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="Deleted" type="xs:boolean" minOccurs="0" />
<xs:element name="Lab_Date" type="xsBig SmileateTime" sqlBig Smileatatype="dateTime" minOccurs="0" />
<xs:element name="Lab_Name" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="Lab_Time" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="Lab_Value" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="PIID" type="xs:int" minOccurs="0" />
<xs:element name="rowguid" msdataBig SmileataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="RowIDGuid" msdataBig SmileataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="SaveDateTime" type="xsBig SmileateTime" sqlBig Smileatatype="dateTime" minOccurs="0" />
<xs:element name="VersionGuid" msdataBig SmileataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="PatientID" msdataBig SmileataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xsTongue Tiedtring" minOccurs="0" />
</xsTongue Tiedequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xsTongue Tiedchema>

Example file: XML file

<?xml version="1.0" standalone="yes"?>
<STG_Additional_Labs>
<Table>
<FacilityID>648</FacilityID>
<Deleted>false</Deleted>
<Lab_Date>2007-05-30T00:00:00-04:00</Lab_Date>
<Lab_Name>Stool Red Subst</Lab_Name>
<Lab_Value>trace</Lab_Value>
<PIID>14</PIID>
<rowguid>03cc9264-8829-464f-b01d-2b18ee4ccdfb</rowguid>
<RowIDGuid>ff9e4e59-6716-46d4-bfcb-61777ed8cf5d</RowIDGuid>
<SaveDateTime>2007-05-30T10:18:24.52-04:00</SaveDateTime>
<VersionGuid>b4b29c65-71e2-425a-9eb9-037acc7ae4d0</VersionGuid>
<PatientID>b4b29c65-71e2-425a-9eb9-037acc7ae4d0</PatientID>
</Table>
<Table>
<FacilityID>648</FacilityID>
<Deleted>false</Deleted>
<Lab_Date>2007-05-19T00:00:00-04:00</Lab_Date>
<Lab_Name>Stool Red Sub</Lab_Name>
<Lab_Value>&lt;0.25/ neg</Lab_Value>
<PIID>14</PIID>
<rowguid>876ca5f9-5c0f-4a74-bf03-2e86260ca2a1</rowguid>
<RowIDGuid>45c45895-8008-40ed-a779-c478d476c15b</RowIDGuid>
<SaveDateTime>2007-05-19T10:13:53.857-04:00</SaveDateTime>
<VersionGuid>b4b29c65-71e2-425a-9eb9-037acc7ae4d0</VersionGuid>
<PatientID>b4b29c65-71e2-425a-9eb9-037acc7ae4d0</PatientID>
</Table>
</STG_Additional_Labs>Ack! Sorry about the emoticons!|||Actually, further work on this problem seemed to reveal that the Cast error was actually due to the connection string in my application being wrong (although it is used for all the other connections in the application). The error now coming back is this:

<?xml version="1.0"?><Result State="FAILED"><Error><HResult>0x80004005</HResult><Description>
<![CDATA[Reference to undeclared namespace prefix: 'sql'.
]]></Description><Source>Schema mapping</Source><Type>FATAL</Type></Error></Result>

What would be the proper way to specify the SQL namespace in the top of the file?|||I have essentially resolved my problem by not trying to output the XML data to then read it in later. I am now using the SQLBulkLoad classes to pump the data from a DataTable to the destination SQL server. It would be nice however to have a method in the SQLReader/Writer that can write out a SQL friendly XSD file, so that it can be used later by the SQLXMLBulkLoad class.

No comments:

Post a Comment