简体   繁体   中英

Column Mapping of type SqlXml in SQLCLR to SQL table Xml column

I am passing in a TempTable to my SQLCLR code with the following schema

-- Schema for temp table

CREATE TABLE ##temp_table_configurationXml_local (
        [OrchConfigXML] [xml]
        )

SQLCLR Code:

       DataTable dt = new DataTable(tableToUse); // tableToUse is the temp table from SQL
       dt.Columns.Add("OrchConfigXML", typeof(SqlXml));


        DataRow dr = dt.NewRow();
        dr["orchConfigXML"] = xmlToUse;  // This is type of SqlXML and has valid XML in it
        dataTableToUse.Rows.Add(dr);

       // Write Data
       // auto-disposable bulk copy operation
       using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
       {
           bulkCopy.DestinationTableName = destTable;
           bulkCopy.ColumnMappings.Add(new 
           SqlBulkCopyColumnMapping("OrchConfigXML", "OrchConfigXML"));
           // Bulk copy operation will drain memory so it is better to setup a batchsize.                      
           bulkCopy.BatchSize = 50000;
           bulkCopy.WriteToServer(dt);
           conn.Close();  //Close the SQL Connection.
       }

While executing the SQL Script and debugging the SQLCLR code I get a error in C#/SQLCLR.

The given ColumnMapping does not match up with any column in the source or destination.

How to Map SQLXML to SQL XML datatype.

Questions:

  1. How many columns are in the destTable ? If just one, then you don't need SqlBulkCopyColumnMapping in the first place.

    (answer: "There is only one column in destTable" )

  2. What are you ultimately trying to do, and why are you using SqlBulkCopy ?

  3. How many rows will be in ##temp_table_configurationXml_local ? If just one, then passing value in via SqlXml input parameter might be better.

  4. Is xmlToUse being added, as another row, to whatever is in ##temp_table_configurationXml_local ? This seems like an odd and/or convoluted setup.

Things to try / consider:

  1. I can't remember, but using column names for the mapping might be case-sensitive, and you have " O rchConfigXML" when you defined the column, but " o rchConfigXML" in the column mapping.

  2. For the column definition in the DataTable , you might want to try using SqlDbType.Xml instead of SqlXml . SqlXml is used for input / output parameters, return types, and result set fields returned by SQLCLR objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM